Uncertain about some Python generator syntax - python

I would like to understand some Python code that I've been reading:
my_stream = some.library.method(arg1=val, arg2=val)(input_stream)
My guess is that some.library.method() returns an iterator into which input_stream is passed as an argument. Is this correct?
I have searched "python generator functions" to get documentation on this type of syntax but have found nothing other than nested examples such as: sum(mult(input)). Can anyone provide an explanation or link?
UPDATE
Below is a specific example:
tokenized_train_stream = trax.data.Tokenize(vocab_file=VOCAB_FILE, vocab_dir=VOCAB_DIR)(train_stream)

Is this correct?
If you are unsure about if thing in python is something you might use inspect built-in module, it provides numerous issomething functions, among them isgenerator, simple usage example
import inspect
lst = [1,2,3]
gen = (i for i in [1,2,3])
print(inspect.isgenerator(lst)) # False
print(inspect.isgenerator(gen)) # True

Related

How to use --force-with-lease with (GitPython) git.remote.Remote?

I am trying to be Pythonic. Thus, I have this code:
import git
import uuid
repo = git.Repo(...)
u = repo.create_remote(uuid.uuid4(), 'https://github.com/...')
If I wasn't trying to be pythonic, I could do this:
repo.git.push(u.name, refspec, '--force-with-lease')
...but I am trying to be Pythonic. How do I do a --force-with-lease push using this (git.remote.Remote) object?
u.push(refspec, help=needed)
It looks like I can say:
u.push(refspec, force=True)
...but I don't think that uses a lease?
After having a quick look at the source code of GitPython, I would guess that it turns _ into - when interpreting the **kwargs. I think the correct way should be:
u.push(refspec, force_with_lease=True)
Based on: function dashify(), which is called from function transform_kwarg().

Is there a lists of built-in methods like (__func__) and how they work in Python 2.7?

I know some of these python built-in methods like: __init__ , __eq__ , __cmp__ . What more are there? Where can I find a good explanation of using these?
You probably want to take a look at:
Python Data Model
A Guide to Python's Magic Methods
Or for Python 3:
Python Data Model
The following answers are not as good as the links above. They do not provide precise lists and they are just for research & fun.
But... for the sport, let's build a code that will give you some basic ones :P
names = [funcname for funcname in dir(object) if funcname.startswith('_')]
for name in names:
print(name)
print(getattr(object, name).__doc__)
print('-' * 20)
Or taking it one step further, listing many special functions:
classes = (eval(i) for i in dir(__builtins__)
if isinstance(eval(i), type) and i != 'type')
magics = {function for one_class in classes for function in dir(one_class)
if function.startswith('__') and function.endswith('__')}
Or, if you want to be insanly rude, you can even run:*
import re
import requests
TYPEOBJECT_URL = 'https://raw.githubusercontent.com/python' \
'/cpython/master/Objects/typeobject.c'
typeobject_c_text = requests.get(TYPEOBJECT_URL).text
print(set(re.findall('__[a-z][a-z0-9_]+__', typeobject_c_text)))
* Probably not all of the functions in the last example are special methods. This is just for the fun.

Difference in how functions are called in Python

I've noticed some functions are called using the var.func() format as in var1.split() while other functions are called using the func(var) format as in sorted(list1).
Here's a very simple program to illustrate the question. I've also noticed the same behavior with open and read functions.
str1 = "This is a string"
list1 = str1.split()
print str1.split(' ')
print sorted(list1)
I'm very new to programming so any help would be greatly appreciated!
Everything in python is an object. Thus when doing something like this:
s = "some string"
s is an str object and you can call all the str methods on it. You can also do things like this:
"some string".split()
and it will give you a list of splitted strings.
This difference has to do with issues of scope. Functions which can be called directly, such as sorted(list1) in your example above, are either builtin functions, or else defined at the top level of one of your imported libraries (for example when using from simpy import *, you can call test() directly to run the built in test suite for the simpy library). Functions which are accessed through the dot operator are functions which are defined for the particular data type that you are applying them to. Remember that each data type in python is an object, and therefore an instance of a class. Those functions, such as split() are defined in that data type's class definition. Additionally, to use the example of test() from the simpy library again, if you were to import a library with only import simpy, you would have to use simpy.test() to call that method.
from simpy import *
test()
vs
import simpy
simpy.test()
The first works because you've imported all methods and classes from the top level of the simpy library, whereas the second works because you've explicitly dived into the scope of the simpy library.
var.func() just means that the function belongs to the object.
For instance, x.sort(). lists (like x) have a function sort.
When you call func(var), func is not a function of lists.
For instance, sorted(x).
This isn't Python specific. You will see the same idea in other languages (e.g. Java).
In var.func() the func() is meant to be used with the var object.
e.g. split() on a string object but cannot use on a list
But func(var) is not confined to a single var object type. you can use it with any appropriate var object.
e.g. sorted() can be used with any iterable like lists, tuples, dicts...
Following TraxusIV's line of thought, I tried the following and it worked!
from string import split
str1 = "This is a string"
list1 = str1.split()
print split(str1)
print sorted(list1)

Removing has_key from HTMLTestRunner

I am using HTMLTestRunner to create an HTML report for my unit test. I suppose the code for HTMLTestRunner provided here is written and optimized for python 2 and before, because I got three errors regarding incompatibility with python 3 like use of StringIO instead of io.
Now, line 639 has a has_key method defined, in this code snippet
def sortResult(self, result_list):
# unittest does not seems to run in any particular order.
# Here at least we want to group them together by class.
rmap = {}
classes = []
for n,t,o,e in result_list:
cls = t.__class__
if not rmap.has_key(cls):
rmap[cls] = []
classes.append(cls)
rmap[cls].append((n,t,o,e))
r = [(cls, rmap[cls]) for cls in classes]
return r
Since python 3 has has_key removed from python 3, so I get error regarding this. Since I am not that much familiar with python, I searched and found that in can be a suitable replacement. So how can I replace this has_ key method? I tried by simply replacing has key with in but it failed and got an invalid syntax error.
Instead of
if not rmap.has_key(cls):
try
if cls not in rmap:
You can see the docs for details.

Eclipse pydev auto-suggestions don't work in some cases

My question is probably stupid and I hope somebody has succeeded in solving this issue.
Sometimes I cannot see right suggestions in auto-completion box (Eclipse 3.5.2, PyDev 1.5.7). For example:
import email
fp = open('my.eml', 'rb')
msg = email.message_from_file(fp)
msg now is a Message object. And functions like get_payload() works fine.
msg.get_payload()
But I don't get get_payload() in auto-completion list.
I think PyDev has no idea of what msg is, so it doesn't know what to show.
Maybe I should import something else, not only email module?
Thanks in advance!
I struggled with this question quite a bit too, until I came across this link. I used the second solution suggested in that link, and it works like a charm.
Basically you need to insert assert isinstance(msg, Message) after you get msg from the function call.
Chances are, the current PyDev build hasn't gone to a point to be able to extract from a function (message_from_file() in your case) to know what kind of object it returns in order to provide auto-completion hinting.
See http://sourceforge.net/projects/pydev/forums/forum/293649/topic/3697707.
Edit: I believe there is interest in PyDev to support the new Python 3 function syntax, PEP 3107, which will solve some of your problems ... in the future.
I know #type in docstring works. As in:
from collections import deque
def foo(a):
''' code completion sample
#type a: deque
'''
return a.popleft() # Code completion will work here
I have not been able to find a way to do it inline within code (except in ways mentioned elsewhere where you simply pretend to assign the variable an instance of a type) as in:
from collections import deque
def foo(a):
''' code completion sample '''
if false: a = deque()
return a.popleft() # Code completion will also work here
But I'm not fond of this method because it probably imposes some performance / code size penalty. I don't know / haven't checked if Python is smart enough to remove this assignment during compile time.
Thanks to SiSoie, here's a link to page explaining possibilities.

Categories