In Python 2.7 one could write:
sum(map(int, "123"))
In Python 3 it became:
sum(x for x in map(int, "123"))
Is there a way to make it more concise?
EDIT:
I was running pylab, which imports sum() from numpy changing semantics of the first snippet. IDLE produces the same results for both Python 2.7 and Python 3.3 as DSM noticed.
NOTE:
I would appreciate anyone down voting this question to leave a comment with a short explanation. I edited the title (originally: Is there a way to get back a concise notation for map() in Python 3?) to reflect my better understanding of underlying issue. What else bothers you?
map still exists in Python 3; in fact, your first code snippet should still work.
The more Python3-ish way of doing it would be
sum(int(d) for d in "123")
though as was pointed out, map() still exists.
Oooops, I found an answer in related questions links:
sum(list(map(int, "123")))
Related
I'm setting up a new algorithm which combines an object detector(bounding box detector) which is in python 3 and a mask generator which is in python 2. The problem here is I have several python 2 files which is required for the mask generation algorithm. So I tried 2to3 to convert all my python 2 files to python 3. The script seemed like working but as it was a deep learning algorithm(for mask generation when bounding box coordinates are given as input) which needs some pytorch weights to be loaded, while testing the model in python 3 the program was throwing out error like
"RuntimeError: Expected object of type torch.FloatTensor but found
type torch.cuda.FloatTensor for argument #2 ‘weight’"
I have searched in PyTorch forums but none of the posts were useful to me. Is it because my mask generation code is trained in python 2 ?
Does that means while loading the weights and testing the model I should use python 2 not python 3 ? It would be great if someone can shed some light on this. As a work around I can still use the object detector code downgraded to python 2. But still I want to know why it was throwing the error.
The problem is(can) that cpu object is expected but it gpu object. Try to put the object to cpu:
mask.cpu()
I just resolved the issue by re-installing the torch(0.4.0) and torchvision(0.2.1) for my conda environment. I had to downgrade the versions of both of them. Finally I was successful in converting my python 2.7 code to python 3. Thanks to 2to3 library. Actually this error was happening in the image normalize function of the PyTorch. That was an internal function which accepts the image array as tensors.
tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
RuntimeError: expected type torch.cuda.FloatTensor but got torch.FloatTensor
I watched the video demo
http://www.lectoro.com/index.php?action=search&ytq=H2O%20TensorFlow%20Deep%20Learning%20Demo
I am able to set up the env using the same spark and sparkling-water versions. The tensorflow runs on python3. Apparently, the example uses python2 code. I am getting SyntaxError: Missing parentheses in call to 'print'. It points to a file with an extreme long path starting with /private ends in context.py.
Is there a way I can get this demo to work with my python3 environment?
yes, right now demo is Python 2 specific. However, we will update it to match Python 3 syntax. I meantime feel free to modify code or look at DeepWater which introduces Deep Learning on top of MxNet (and TF, and Caffe - in progress) https://github.com/h2oai/deepwater
Assuming you are using this python notebook: https://github.com/h2oai/sparkling-water/blob/master/py/examples/notebooks/TensorFlowDeepLearning.ipynb
The changes for Python 3:
In [8] put parantheses around:
print( [c.dim for c in H2O_w] )
print( [c.dim for c in H2O_b] )
In [3] add a list() around the use of range():
sc.parallelize(list(range(NODES)), NODES).map(map_fun).collect()
And [4]:
self._x = list( range(784) )
(I notice this change had already been done in the call to train().)
I couldn't spot anything else, and those changes should be compatible with Python 2. If you still get errors can you post in which section of the notebook that the error happens in?
I am very new to antlr 4 and my target language is PYTHON2.
I am not able to understand CommonTokenStream in python and how I can access tokens in antlr 4.
What I require is to access tokens present in Hidden Channel ?
Please someone point me to some proper documentation where I can understand how to access tokens and manipulate them in python.
I am sorry if the question is vague, I am new here.
The ANTLR book is one.
https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference
In the chapter 12 "Wielding Lexical Black Magic", it has "Accessing hidden channel" section. Use the TokenStreamRewriter to rewrite the token.
You need to mentally convert Java code in the book to Python code. The runtime libraries have subtle differences but they are virtually the same.
It is not the only way. You can override lexer's emit() function (which I usually do). Then you have total control over the token routing.
If you are on python 3 it is all wonderfully done cooked and ready
https://github.com/jszheng/py3antlr4book
For Some Python start hints try
https://github.com/antlr/antlr4/blob/master/doc/python-target.md
If you are using Anaconda3 try egrep of class def import comments(#) of all *.py
Anaconda3\Lib\site-packages\antlr4_python3_runtime-4.7.1-py3.6.egg\antlr4
Or even write a ANTLR script to create the python docs and share with me and the world
Also at run time this helps to see what methods and properties are in say a CTX object
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
print("-------------------------------------------")
dump(ctx)
print("===========================================")
just wondering what does the following command means? so hard to google topics related to pig:
pig -Dpig.usenewlogicalplan=false
i ran the pig script in map/reduce mode, it failed, by adding that flag(-D), it worked, but still have some issue. so what does it mean anyways? Thanks.
The larger problem is described here: PIG-1731
It looks like a work around is for you to break your FILTERs out onto multiple lines.
Pig 0.8.0 introduced some bugs in the logical optimizer. The -Dpig.usenewlogicalplan=false option forces a fallback to the earlier version. Alternatively upgrading to 0.8.1+ should also solve the problem.
Kindly see this for more information
https://books.google.com/books?id=RG-v6qUktSYC&pg=PA96&lpg=PA96&dq=pig.usenewlogicalplan&source=bl&ots=tRH-GSTEkD&sig=z76hSCS036JqQT26zM9lKrXjx-s&hl=en&sa=X&ved=0CD8Q6AEwA2oVChMIrfj2h-DXyAIVBs5jCh3vTQ7z#v=onepage&q=pig.usenewlogicalplan&f=false
https://cwiki.apache.org/confluence/display/PIG/Pig+0.9+Backward+Compatibility
I have the following python 3 file:
import base64
import xxx
str = xxx.GetString()
str2 = base64.b64encode(str.encode())
str3 = str2.decode()
print str3
xxx is a module exported by some C++ code. This script does not work because calling Py_InitModule on this script returns NULL. The weird thing is if I create a stub xxx.py in the same directory
def GetString() :
return "test"
and run the original script under python.exe, it works and outputs the base64 string. My question is why doesn't it like the return value of xxx.GetString? In the C++ code, it returns a string object. I hope I have explained my question well enough... this is a strange error.
I know everybody says this...but:
Boost has an awesome library for exposing classes to python and getting data to and fro. If you're having problems, and looking for alternatives is an option I'd highly recommend the boost python library of the C interface. I've used them both, boost wins hands down imo.
Py_InitModule() is for initializing extension modules written in C, which is not what you are looking for here. If you want to import a module from C, there is a wealth of functions available in the C API: http://docs.python.org/c-api/import.html
But if your aim is really to run a script rather than import a module, you could also use one of the PyRun_XXX() functions described here: http://docs.python.org/c-api/veryhigh.html
Er... You have to investigate why Py_InitModule returns NULL. Posting the Python code using that module won't help.