I read PDF books on my tab, and I tend to pick up the one that has not many pages though this is not always the case.
Sometimes I do pick up what interest me, however few months back I wrote a python script to get total number of pages of all the pdf ebooks I have in folder.
Today I was stumbling across web pages and some how I reachedSO API page. There are plenty of ways to do all sorts of things, but I quickly got all the tags I am subscribed to using the Json data they return.
Before I go over getting rid of pyc files I think it would be fair to go over when pyc are created ! The .pyc files are created (and possibly overwritten) only when that python file is imported by some other script. If the import is called, Python checks to see if the .pyc file's internal time stamp matches the corresponding .py file. If it does, it loads the .pyc; if it does not or if the .pyc does not yet exist, Python compiles the .py file into a .pyc and loads it.
Although it always helps to have pyc files since CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py , CPython caches the byte code in a foo.pyc file right next to the source. From PEP-3147
If your source changes; CPython will recompile and re-cached the bytecode.
But sometimes you would still don't want to have a pyc or a file ending with ~
to avoid that I came up with an shell command or alternatively a python script to get rid of pyc or files ending with '~' tilde .
Find files ending with pyc or ~
below code will find and delete the files as well.
click read more for a little romance with python :p
I have been long trying to get this thing figure out how to fetch a url while sitting behind proxy. I took a bit of help from SO, now I got it figured how it works using python that too with authentication. Here is basic sample code.
getattr is a python built-in function taking (at least) two arguments: the object from which you're getting the attribute, and the string name of the attribute.
If the string name is a constant, say 'foo', getattr(obj, 'foo') is exactly the same thing as obj.foo.
So, the main use case for the built-in function getattr is when you don't have the attribute name as a constant, but rather as a variable. A second important use case is when you pass it three arguments, rather than just two: in that case, if the attribute is absent from the object, getattrreturns the third, "default", argument, rather than raising an exception.
__getattr__ is a special method, defined in a class, that gets invoked when some attribute of an instance of that class is requested, and other normal ways to supply that attribute (via the instance's __dict__, slots, properties, and so on) all failed. You can define it, for example, when you want to delegate otherwise-undefined attribute lookups to other objects.
So your second example is wrong because the builtin getattr can never be called with a single argument.
Using the above model, we can get the attribute by supplying the attribute_name. This works when we give a name as a string and you need to look up the instance attribute referenced by that name.
Use of __getattr__
You can also tell a class how to deal with attributes which it doesn't explicitly manage and do that via __getattr__ method.
Python will call this method whenever you request an attribute that hasn't already been defined. So you can define what to do with it.
Caveats and use of __getattribute__
If you need to catch every attribute regardless whether it exists or not, use __getattribute__ instead.
Difference__getattr__ only gets called for attributes that don't actually exist. If you set the attribute directly, referencing that attribute will retrieve it without calling __getattr__.
__getattribute__ is called all the times.
A key difference between __getattr__ and __getattribute__ is that __getattr__ is only invoked if the attribute wasn't found the usual ways. It's good for implementing a fallback for missing attributes, and is probably the one of two you want.
more bits and pieces : http://www.devx.com/opensource/Article/31482/0/page/4
Today I came across an article on linked in Pulse by Ted Eagly on how to live always learning. i.e. Remaining Teachable: The Never-Ending Path To Wisdom.
He points in the beginning what Leo Tolstoy said
"The most difficult subjects can be explained to the most slow-witted man if he has not formed any idea of them already; but the simplest thing cannot be made clear to the most intelligent man if he is firmly persuaded that he knows already.”
I didn't knew somebody wise has already made statement that is exactly what I have been thinking about. No doubt I am now losing extra wisdom tooth.
I used to and on many occasion still come across people who say "I know" but when they say that they are not careful enough what that might lead to when they say "I know". It is good to know and knowing is everything, but listening to others is also equally important. With the attitude to not listen or ignore with over confidence or persuade on a belief that you know it right and better than the other person will shun your ability to judge and make better decision. If a person chooses not to listen and continues to flap his wings of his righteousness would ultimately become outdated of what is right (for himself or for his fellows).
Ignorance is not bliss, Always be open to listen to others.
I used to call this thoughts eclipsing thinking, because such an attitude limits your thinking, devouring the person from the path of progress or may totally lead to misery.
I would want to be fierce and point what type of people and what people say when their thoughts eclipse thinking.
The first category of people are the one I want to point are those who are young learners with little experience but when they get an exposure and opportunity to sit among the smart and wise people, they develop the attitude of being masters of knowledge beholder, a bit of arrogance kicks in. We usually refer to these people smart ass in urban language.
The second one are those who are really smart and keeps good knowledge, they know but knowing shouldn't limits listening to others. And in a self setup of thoughts ignoring that the world around is evolving and with the change in information, knowledge requires updating. Those in this category are also on the fall of devouring themselves from the latest knowledge of the ever evolving world around and keeps the attitude but lesser than the first category. I really respect these people, since they are smart and I try to handle the conversation and get along with a delicacy.
The third category is of those people actually knows nothing but likes to speak a lot, these are the stupid liars who are fooling around the management or being outspoken and heard by everyone. Their tendency is just to prove themselves that the know everything but the truth is the either keep half knowledge of what they do or they don't know anything at all. I tend to stay away from these people and don't be friend be with them. (these are not the one what Leo Tolstoy or Ted Eagly was referring to.)
The fourth category of people are the one who are on high profile job designation, these people are more equipped with arrogance than attitude. But as I have noticed this fourth category is easier to be fooled by those people in third category. The only reason why their thinking is limited is their tendency that they know better ignoring the fact that the person they do not want to listen to may have right or new knowledge or information that could be useful.
Thanks for reading, if this was such a torture please don't hesitate swearing in the comment box below, I am also not very experienced in writing but I only write with what I have lived and experienced .
magic of collections namedtuple and make the object to be used in a manner similar to a tuple or list
OMFG, just one of my discovery how we can have class object behave like its of list type when it is actually not list.
From PMOTW, namedtuple instances are just as memory efficient as regular tuples because they do not have per-instance dictionaries. Each kind of namedtuple is represented by its own class, created by using the namedtuple() factory function. The arguments are the name of the new class and a string containing the names of the elements.
collections.namedtuple docs: https://docs.python.org/2/library/collections.html
In the below code we make use of namedtuple and make class object behave like a list or tuple using __getitem__ and update the value of arguments using __setitem__ .
It has really taken me a while to understand Python decoraters, one of the reason is I never felt the use of it, I feel its just different way of programming, I have never written decoraters in all the cakes I have baked till now, but sometimes I get across the piece of cake written by someone else. And this is how I have learned and some bits and pieces over the internet.
So to settle down on my understanding and if it can help you as well I am writing up this example.
I have been waiting for so long to understand how logging works using logging configuration file, So in this post I will just be focussing on logging configuration file how we can log message to shell as well as disk using logging.StreamHandler Class and logging.FileHandler Class.
The user of the logging package makes logging calls on instances of Logger, which are organized into a hierarchy based on a "dotted name" namespace. This hierarchy is embodied in an encapsulated singleton Manager instance (which can be ignored by users of the package, for most purposes). Based on the type of logging call and the logging configuration (see below)
So First just logging to terminal.
the call may be passed through a set of Filter instances to decide whether it should be dropped. If not, then the logger consults a set of Handler instances which are associated with it, and asks each handler instance to "handle" the logging event. By default, the system moves up the namespace hierarchy and invokes handlers on all loggers at or above the level of the logger on which the logging call was made. (You can override this by setting a logger's "propagate" attribute to 0 - no traversal up the hierarchy is made from such a logger.
Now I will move on to logging to shell as well as file simultaneously. In the below example of logging configuration file I will comment the description of loggers, Formatters, Handlers etc.
awakened by thoughts,