Re: [BangPypers] Favorite tips/techniques

2013-09-13 Thread Saager Mhatre
On Sep 9, 2013 8:51 PM, "CsquaredinOmaha" wrote: > > > For a while I had thought it would be interesting to hear "tips/techniques you > find yourself often using" - or perhaps found useful at one point (and thus would be valuable to newbies). > > It could be simple snippet, or some description of

Re: [BangPypers] Favorite tips/techniques

2013-09-13 Thread Saager Mhatre
On Sep 9, 2013 8:51 PM, "CsquaredinOmaha" wrote: > > > For a while I had thought it would be interesting to hear "tips/techniques you > find yourself often using" - or perhaps found useful at one point (and thus would be valuable to newbies). > > It could be simple snippet, or some description of

Re: [BangPypers] Favorite tips/techniques

2013-09-13 Thread Saju M
Saager, As per python module-import semantics, sub-modules don't end up as names in the package-module[*] unless explicitly added. I didn't get it, what you mean by package-module[*] ?. -- One simple test. dir(json) showing the module "tool" after "from json import tool"

Re: [BangPypers] Favorite tips/techniques

2013-09-13 Thread Dhananjay Nene
On Fri, Sep 13, 2013 at 12:49 PM, Saju M wrote: > Import sub-modules with alias. > Is it a bug ? > import json from json import tool > > > import json as sjson from sjson import tool > Traceback (most recent call last): > File "", line 1, in > ImportE

Re: [BangPypers] Favorite tips/techniques

2013-09-13 Thread Saju M
Import sub-modules with alias. Is it a bug ? >>> >>> import json >>> from json import tool >>> >>> >>> import json as sjson >>> from sjson import tool Traceback (most recent call last): File "", line 1, in ImportError: No module named sjson >>> ___

Re: [BangPypers] Favorite tips/techniques

2013-09-12 Thread Noufal Ibrahim
I often type this x = "Break this into a list".split() instead of x = ["Break", "this", "into", "a", "list"] The latter is more tedious to type. -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https:/

Re: [BangPypers] Favorite tips/techniques

2013-09-12 Thread L Radhakrishna Rao
Not so complex, but the technique which I love is the comprehension, it decreases the code size. On Fri, Sep 13, 2013 at 8:25 AM, Noufal Ibrahim wrote: > Saager Mhatre writes: > > > On Tue, Sep 10, 2013 at 4:54 PM, Noufal Ibrahim >wrote: > > > >> Saju M writes: > >> [...] > >> > > > >> > * Wh

Re: [BangPypers] Favorite tips/techniques

2013-09-12 Thread Noufal Ibrahim
Saager Mhatre writes: > On Tue, Sep 10, 2013 at 4:54 PM, Noufal Ibrahim wrote: > >> Saju M writes: >> [...] >> > >> > * Why dir(json) not showing "tool" ?? >> >> Because of the __all__ variable in the module. >> http://docs.python.org/2/tutorial/modules.html >> > > Noufal, > Well, not quite, rig

Re: [BangPypers] Favorite tips/techniques

2013-09-12 Thread Saager Mhatre
On Tue, Sep 10, 2013 at 4:54 PM, Noufal Ibrahim wrote: > Saju M writes: > [...] > > > * Why dir(json) not showing "tool" ?? > > Because of the __all__ variable in the module. > http://docs.python.org/2/tutorial/modules.html > Noufal, Well, not quite, right? I posit it's basically because as per

Re: [BangPypers] Favorite tips/techniques

2013-09-12 Thread Saager Mhatre
On Tue, Sep 10, 2013 at 10:39 AM, Shabda Raaj wrote: > > https://github.com/webpy/webpy/blob/master/web/utils.py#L52 > > Wow, thats better than the "bare" bunch impl. Gonna use it now. It's just s much nicer when the map/dict in your platform

Re: [BangPypers] Favorite tips/techniques

2013-09-11 Thread Amit Sethi
1. I do like the accessing dict attributes as keys and specifically where they make more sense as structure by itselfI do use class ABC(object): a = '' def __init__(self, **kwargs): self.__dict__.update(**kwargs) 2. When creating a dictionary of constants create it dynamica

Re: [BangPypers] Favorite tips/techniques

2013-09-11 Thread s|s
There are times when the project requires a large blob of data which needs to be loaded with the python code. A general practice is to use pickle and json object which is opened using file functions etc. I tend to convert this kind of data (depending on size) into python file using pprint and stor

Re: [BangPypers] Favorite tips/techniques

2013-09-11 Thread Anand Chitipothu
Another script I use often is repr. https://github.com/anandology/hacks/blob/master/repr It reads each line from stdin and prints it as python repr. Useful to see hidden/non-alphanumeric characters. $ echo -e "a\bc" c $ echo -e "a\bc" | repr a\x08c\n This is similar to od, but od prints fixed n

Re: [BangPypers] Favorite tips/techniques

2013-09-11 Thread s|s
This use case where one needs to keep related values together as logical group: Properties, getattr, setattr are quite interesting option but I prefer collections.namedtuple Example EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csvfor emp in map(E

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Gopalakrishnan Subramani
Attributes are stylish, readable, feel native as object compared to dictionary. Sometimes I use setter methods to override the default assignments, modify getter to format the values and getter for virtual attributes for DSL stuffs. class DictObj(dict): def __getattr__(self, key): if

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread BibhasD
I'm just curious. I faced a similar issue before while answering a question about Django on StackOverflow. Django turns context variable dictionaries into objects with attributes in template. So in one question in SO someone posted this question. Had to answer that Django doesn't really support dis

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Dhananjay Nene
Ignoring classes for the moment, how likely do you think you would have a dict like that :) On a separate note if you are using primitive types, I cannot think of any scenarios, where not coercing keys to be of the same type would be considered inappropriate (except in case of reverse dicts) On T

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread CsquaredinOmaha
). Regards, Chris From: "Me@Bibhas" To: Bangalore Python Users Group - India Sent: Tuesday, September 10, 2013 4:37 AM Subject: Re: [BangPypers] Favorite tips/techniques This thread is awesome. Keep them coming. :) I've also been usin

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Shabda Raaj
> I personally like to use IPython. > To see all variables, constants, modules. Ipython is amazing. To see a library code, I jsut tab-out lib_name.__file__ and then !vim paste_the_file_path Going to add something like anand's pyvi to ipy_user_conf.py. Ipython with treat anything after bang(!)

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Ramchandra Apte
Obligatory comic: https://xkcd.com/936/ On 10 September 2013 10:42, Anand Chitipothu wrote: > On Tue, Sep 10, 2013 at 10:39 AM, Shabda Raaj wrote: > > > > https://github.com/webpy/webpy/blob/master/web/utils.py#L52 > > > > Wow, thats better than the "bare" bunch impl. Gonna use it now. > > > >

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Sreekandh Balakrishnan
> This thread is awesome. Keep them coming. :) Prettyprint comes very handy when it comes to object debugging. Use it in my logger method always ;) http://docs.python.org/2/library/pprint.html ___ BangPypers mailing list BangPypers@python.org https:/

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread kracekumar ramaraju
I personally like to use IPython. To see all variables, constants, modules. n [9]: import requests In [10]: requests. requests.ConnectionError requests.api requests.models requests.HTTPError requests.auth requests.options requests.NullHandler requests.c

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Anand Chitipothu
Another utility script that I use very heavily, pyvi. https://github.com/anandology/hacks/blob/master/pyvi $ pyvi json.tool That opens json.tool module (or any other module) in vim. It also changes the current dir to that module directory so that you can easily open other modules in the same pa

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Vineet Naik
On Tue, Sep 10, 2013 at 4:48 PM, Saju M wrote: > Hi, > I have couple of doubts > > > * How do you find "json" has module named "tool" ?. > > > * Why dir(json) not showing "tool" ?? > > >>> import json > >>> dir(json) > ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', > '__d

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Noufal Ibrahim
Saju M writes: > Hi, > I have couple of doubts > > > * How do you find "json" has module named "tool" ?. I look at the code/docs. > > > * Why dir(json) not showing "tool" ?? Because of the __all__ variable in the module. http://docs.python.org/2/tutorial/modules.html import json d

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Saju M
Hi, I have couple of doubts * How do you find "json" has module named "tool" ?. * Why dir(json) not showing "tool" ?? >>> import json >>> dir(json) ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Noufal Ibrahim
Vineet Naik writes: [...] > If you are using jedi[1] with auto-complete in emacs, C-. takes you to > the definition by opening the module file in a write protected > buffer. I use jedi mainly for this feature and inline documentation > popout rather than for autocomplete :-) [...] I'll check

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Vineet Naik
On Tue, Sep 10, 2013 at 4:21 PM, Noufal Ibrahim wrote: > Saju M writes: > > > echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m > json.tool > > > > In this command, what is this "json.tool" ? > > > > I could not find "tool" in dir(json) > > > import json > dir(json) > >

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread BibhasD
On Tuesday 10 September 2013 04:16 PM, Saju M wrote: > echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m json.tool > > In this command, what is this "json.tool" ? > > I could not find "tool" in dir(json) > import json dir(json) > ['JSONDecoder', 'JSONEncoder', '__all__',

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Vineet Naik
On Tue, Sep 10, 2013 at 4:16 PM, Saju M wrote: > echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m > json.tool > > In this command, what is this "json.tool" ? > > I could not find "tool" in dir(json) > > >>> import json > >>> dir(json) > ['JSONDecoder', 'JSONEncoder', '__all__',

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Noufal Ibrahim
Saju M writes: > echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m json.tool > > In this command, what is this "json.tool" ? > > I could not find "tool" in dir(json) > import json dir(json) > ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', > '__d

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Jeffrey Jose
I've always found attribute access of dictionary a bit weird, and too much Javascript-y. The latter by itself doesnt make it bad .. but something I dont prefer generally. And yes, property access wont work for keys that start with numbers. (Same in JS) -jeff On Tue, Sep 10, 2013 at 3:52 PM, Me@

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Saju M
echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m json.tool In this command, what is this "json.tool" ? I could not find "tool" in dir(json) >>> import json >>> dir(json) ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Navin Pai
Date: Tue, 10 Sep 2013 15:39:44 +0530 > From: Vineet Naik > To: Bangalore Python Users Group - India > Message-ID: > g1u3z80zgnt6cg3lenrsb...@mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Command line json formatter > $ echo '{"name": "Bangpypers", "location": "Bang

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Me@Bibhas
What would happen for a dictionary like this? >>> d = {'1': 'foo', 1: 'bar'} >>> d {'1': 'foo', 1: 'bar'} On Tuesday 10 September 2013 10:00 AM, Noufal Ibrahim wrote: > Shabda Raaj writes: > >> http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ >> >> W

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Gopalakrishnan Subramani
Not all keys in the dictionary can be expressed as attributes. Attributes has to follow the naming conventions. I had problems with user generated 'keys', JSON supported keys. On Tue, Sep 10, 2013 at 2:41 PM, Mandar Vaze / मंदार वझे < mandarv...@gmail.com> wrote: > Both Shabda and Nouful u

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Vineet Naik
Command line json formatter $ echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m json.tool On Tue, Sep 10, 2013 at 3:28 PM, Noufal Ibrahim wrote: > "Me@Bibhas" writes: > > > Don't know if I can call it a snippet, But this command on terminal - > > > > $ python -m SimpleHTTPServe

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Shabda Raaj
Another tip: Its common to write decorators which clober the docstring and other meta data. If you use functools.wrap this metadata is preserved. http://docs.python.org/2/library/functools.html https://github.com/django/django/blob/master/django/contrib/auth/decorators.py#L19 On Tue, Sep 10, 2

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Anand B Pillai
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Tuesday 10 September 2013 03:28 PM, Noufal Ibrahim wrote: > "Me@Bibhas" writes: > >> Don't know if I can call it a snippet, But this command on >> terminal - >> >> $ python -m SimpleHTTPServer 8080 > > Similar but less well known. > > Command l

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Anand B Pillai
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Tuesday 10 September 2013 03:07 PM, Me@Bibhas wrote: > This thread is awesome. Keep them coming. :) > > I've also been using random string generator Shabda posted for a > long time. Handy. > > On Tuesday 10 September 2013 12:48 PM, Lakshman Prasad

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Noufal Ibrahim
"Me@Bibhas" writes: > Don't know if I can call it a snippet, But this command on terminal - > > $ python -m SimpleHTTPServer 8080 Similar but less well known. Command line ftp client (similar to ftp(1)) python -m ftplib ftp.gnu.org Command line mail client (similar to mail(1)) but needs a loca

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Me@Bibhas
Don't know if I can call it a snippet, But this command on terminal - $ python -m SimpleHTTPServer 8080 is really handy for sharing files(along with the use of `localtunnel` maybe) and testing HTML/CSS. On Tuesday 10 September 2013 12:48 PM, Lakshman Prasad wrote: > My first random password (unt

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Me@Bibhas
This thread is awesome. Keep them coming. :) I've also been using random string generator Shabda posted for a long time. Handy. On Tuesday 10 September 2013 12:48 PM, Lakshman Prasad wrote: > My first random password (until it is replaced with SHA, MD5, bCrypt, > whatever): > >> str(random.random

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Mandar Vaze / मंदार वझे
Both Shabda and Nouful used the term "prefer" Is there a "best practice" ? http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python This guy asked for "pitfalls and caveats" - but no one seem to have addressed the "question" (There are several answers about "How

Re: [BangPypers] Favorite tips/techniques

2013-09-10 Thread Lakshman Prasad
My first random password (until it is replaced with SHA, MD5, bCrypt, whatever): > str(random.random())[2:] '742557965797' On Tue, Sep 10, 2013 at 11:34 AM, Vinayak Hegde wrote: > On Tue, Sep 10, 2013 at 10:45 AM, Noufal Ibrahim >wrote: > > > Anand Chitipothu writes: > > > > [...] > > > > >

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Vinayak Hegde
On Tue, Sep 10, 2013 at 10:45 AM, Noufal Ibrahim wrote: > Anand Chitipothu writes: > > [...] > > > I use it very often. Here is my random-password script. > > [...] > > I use mkpasswd(1) :) > What ever you use, please use py-bcrypt or something similar before you store it in the database. Here

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Noufal Ibrahim
Anand Chitipothu writes: [...] > $ sudo apt-cache search mkpasswd > libstring-mkpasswd-perl - Perl module implementing a random password > generator I think that's something else. noufal@sanitarium% dpkg -S =mkpasswd whois: /usr/bin/mkpasswd noufal@sanitarium% file =mkpasswd /usr/bin/mkpas

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Shabda Raaj
Real programmers pipe /dev/urandom :) Let me preempt the xkcd: http://xkcd.com/378/ On Tue, Sep 10, 2013 at 10:47 AM, Anand Chitipothu wrote: > > On Tue, Sep 10, 2013 at 10:45 AM, Noufal Ibrahim > wrote: > >> Anand Chitipothu writes: >> >> [...] >> >> > I use it very often. Here is my random-

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Anand Chitipothu
On Tue, Sep 10, 2013 at 10:45 AM, Noufal Ibrahim wrote: > Anand Chitipothu writes: > > [...] > > > I use it very often. Here is my random-password script. > > [...] > > I use mkpasswd(1) :) > $ sudo apt-cache search mkpasswd libstring-mkpasswd-perl - Perl module implementing a random password ge

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Noufal Ibrahim
Anand Chitipothu writes: [...] > I use it very often. Here is my random-password script. [...] I use mkpasswd(1) :) -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listi

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Anand Chitipothu
On Tue, Sep 10, 2013 at 10:39 AM, Shabda Raaj wrote: > > https://github.com/webpy/webpy/blob/master/web/utils.py#L52 > > Wow, thats better than the "bare" bunch impl. Gonna use it now. > > Unrelated tip: > > Here is a one liner I use to generate passwords and other random strings. > > ''.join(ran

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Shabda Raaj
> https://github.com/webpy/webpy/blob/master/web/utils.py#L52 Wow, thats better than the "bare" bunch impl. Gonna use it now. Unrelated tip: Here is a one liner I use to generate passwords and other random strings. ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Anand Chitipothu
On Tue, Sep 10, 2013 at 10:09 AM, Noufal Ibrahim wrote: > Shabda Raaj writes: > > >> I generally like to use attributes instead of keys. > > > > If you are parsing json, aren't you limited to using keys? > > Of course. I was making a general statement about attributes vs. keys. > > > The bunch pa

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Noufal Ibrahim
Shabda Raaj writes: >> I generally like to use attributes instead of keys. > > If you are parsing json, aren't you limited to using keys? Of course. I was making a general statement about attributes vs. keys. > The bunch pattern can fix this, but its not widely known/used, so I > don't use it

[BangPypers] Favorite tips/techniques

2013-09-09 Thread Shabda Raaj
> I generally like to use attributes instead of keys. If you are parsing json, aren't you limited to using keys? The bunch pattern can fix this, but its not widely known/used, so I don't use it as frequently as I would like. -- Thanks, Shabda Agiliq.com - Building Amazing Apps agiliq.com/blog/

Re: [BangPypers] Favorite tips/techniques

2013-09-09 Thread Noufal Ibrahim
Shabda Raaj writes: > http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ > > With api responses after you have parsed the json, you start doing things > like: > > api_response["attribute"] > > I would much prefer to do > > api_response.attribute I gener

[BangPypers] Favorite tips/techniques

2013-09-09 Thread Shabda Raaj
http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ With api responses after you have parsed the json, you start doing things like: api_response["attribute"] I would much prefer to do api_response.attribute Which the bunch pattern can enable. I don't u

[BangPypers] Favorite tips/techniques

2013-09-09 Thread CsquaredinOmaha
  For a while I had thought it would be interesting to hear "tips/techniques you  find yourself often using"  - or perhaps found useful at one point (and thus would be valuable to newbies). It could be simple snippet, or some description of  logic, technique or steps. >From simple to sophisticate