python-2.x

Tensorflow: Why is tf.case giving me the wrong result?

一曲冷凌霜 提交于 2019-12-23 20:28:39
问题 I'm trying to use tf.case (https://www.tensorflow.org/api_docs/python/tf/case) to conditionally update a Tensor. As shown, I'm trying to update learning_rate to 0.01 when global_step == 2 , and to 0.001 when global_step == 4 . However, when global_step == 2 , I already get learning_rate = 0.001 . Upon further inspection, it looks like tf.case is giving me the wrong result when global_step == 2 (I get 0.001 instead of 0.01 ). This is happening even though the predicate for 0.01 is evaluating

What is the Pythonic way to avoid reference before assignment errors in enclosing scopes?

帅比萌擦擦* 提交于 2019-12-23 13:56:33
问题 I'm speaking about the general case. Here's an example: c = 1 def a(): def b(): print(c) b() c = 2 a() This code will return the following error: NameError: free variable 'c' referenced before assignment in enclosing scope . While the logical assumption is that the output should be 1 . What is the Pythonic solution to this issue? Use the global or nonlocal statements (which I don't like)? Maybe just avoid such situations, where multiple scopes share variables with identical names? 回答1:

Remove doc strings but not asserts from compiled CPython

好久不见. 提交于 2019-12-23 10:24:22
问题 I want to remove doc strings from a Python program but leave asserts (and __debug__ sections) in. I have been using the -OO flag to generate .pyo files but according to the documentation that removes both asserts and doc strings. I'm using CPython 2.7. Clarification: I'm removing the docstrings as a cheap obfuscation method. Management made that decision and I think whether that is a useful thing to do is outside the scope of this question. 回答1: You can't have half of -O . It removes

PyGame Sprites Occasionally Not Drawing

会有一股神秘感。 提交于 2019-12-23 04:37:12
问题 I am trying to make a basic card game using PyGame. I am currently just trying to draw a single card to the screen. The weird thing is, occasionally it will draw and occasionally it won't. Below is my code: import pygame from pygame.locals import * from socket import * import sys import os import math import getopt import random def load_png(name) : # Loads an image and returns the image object fullname = os.path.join('/home/edge/Downloads/Playing Cards/PNG-cards-1.3', name) image = pygame

Getting more control over the rich comparison operators in python2

北慕城南 提交于 2019-12-22 14:07:12
问题 >>> class Yeah(object): ... def __eq__(self, other): ... return True ... >>> class Nah(object): ... def __eq__(self, other): ... return False ... >>> y = Yeah() >>> n = Nah() >>> y == n True >>> n == y False The left guy wins because when python2 sees x == y it tries x.__eq__(y) first. Is there any way to modify Nah so that he will win both times? My use-case is making something like this: class EqualsAnyDatetime(object): def __eq__(self, other): return isinstance(other, datetime) It just

how to investigate python2 segfault on imp.load_module

廉价感情. 提交于 2019-12-22 12:08:11
问题 I am trying to install and use dolfin on Arch Linux, with Python 2.7.3. What is the best way to find out what is causing segmentation faults such as these? $ python2 -c "import dolfin; print dolfin.__version__" [3] 6491 segmentation fault (core dumped) python2 -c "import dolfin; print dolfin.__version__" I have tried inspecting the core file through gdb: $ sudo systemd-coredumpctl gdb 6491 but they are always truncated: Reading symbols from /usr/bin/python2.7...(no debugging symbols found)..

listdir doesn't print non-english letters correctly

南笙酒味 提交于 2019-12-22 08:45:00
问题 On Python 2.7, for dir in os.listdir("E:/Library/Documents/Old - Archives/Case"): print dir prints out: Danny.xlsx Dannyh.xlsx ~$??? ?? ?????? ??? ???? ???????.docx while this: # using a unicode literal for dir in os.listdir(u"E:/Library/Documents/Old - Archives/Case"): print dir prints out: Dan.xlsx Dann.xlsx Traceback (most recent call last): File "E:\...\FirstModule.py", line 31, in <module> print dir File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode return codecs.charmap

What ordering does dict.keys() and dict.values() guarantee? [duplicate]

纵饮孤独 提交于 2019-12-22 04:53:09
问题 This question already has answers here : Python dictionary: are keys() and values() always the same order? (8 answers) Closed 2 years ago . This question arises from this answer where one user uses d.keys() and d.values() separately to initialise a dataframe. It's common knowledge that dictionaries in python versions under 3.6 are not ordered. Consider a generic dictionary of the form: d = {k1 : v1, k2 : v2, k3 : v3} Where the keys k* are any hashable objects, and the values v* being any

How to check if object is instance of new-style user-defined class?

拥有回忆 提交于 2019-12-22 04:06:16
问题 Code: import types class C(object): pass c = C() print(isinstance(c, types.InstanceType)) Output: False What correct way to check if object is instance of user-defined class for new-style classes? UPD: I want put additional emphasize on if checking if type of object is user-defined . According to docs: types.InstanceType The type of instances of user-defined classes. UPD2: Alright - not "correct" ways are OK too. UPD3: Also noticed that there is no type for set in module types 回答1: You can

How to check if object is instance of new-style user-defined class?

房东的猫 提交于 2019-12-22 04:05:03
问题 Code: import types class C(object): pass c = C() print(isinstance(c, types.InstanceType)) Output: False What correct way to check if object is instance of user-defined class for new-style classes? UPD: I want put additional emphasize on if checking if type of object is user-defined . According to docs: types.InstanceType The type of instances of user-defined classes. UPD2: Alright - not "correct" ways are OK too. UPD3: Also noticed that there is no type for set in module types 回答1: You can