x=random.random(), TypeError:'module' object is not callable - python-2.x

I have tried to debug this program but I don't know what the error refers to. When I run my code I get this error.
enter image description here
here is my code that you can refer to:
import urllib
fh=urllib.urlopen("http://www.py4inf.com/code/romeo.txt")
for line in fh:
print line.strip()
any help will be apprriciated

the bug was that in my folder I had named a file "random.py" and used "import random " in this file. So I changed the name of the file and re-ran my code.It worked. It was a silly mistake on my part.

Related

Using Python shutil.copy to overwrite a file gives asyntax error

I am trying to overwrite one .png file with another using shutil.copy, but I keep getting a syntax error. The line in question is-
shutil.copy("/var/www/vk7krj/running/yoga.png", "/var/www/vk7krj/running/run4.png")
and the error is-
File "./latlong4.py", line 63
shutil.copy("/var/www/vk7krj/running/yoga.png","/var/www/vk7krj/running/run4.png")
SyntaxError: invalid syntax
with an up-arrow under the l in shutil
I have "import shutil" and also "from shutil import copyfile" in the head of the file.
Several hours of google searching has turned up nothing, so any assistance would be great.
Problem solved- turns out the error was incorrect indentation in an if-else earlier in the script- corrected that and the syntax error went away.

Can't use io.open in home directory - Lua

I'm writing a Mac OS program, and I have the following lines:
os.execute("cd ~/testdir")
configfile = io.open("configfile.cfg", "w")
configfile:write("hello")
configfile:close()
The problem is, it only creates the configfile in the scripts current directory instead of the folder I have just cd' into. I realised this is because I'm using a console command to change directory, then direct Lua code to write the file. To combat this I changed the code to this:
configfile = io.open("~/testdir/configfile.cfg", "w")
However I get the following result:
lua: ifontinst.lua:22: attempt to index global 'configfile' (a nil value)
stack traceback:
ifontinst.lua:22: in main chunk
My question is, what's the correct way to use IO.Open to create a file in a folder I have just created in the users home directory?
I appreciate I'm making a rookie mistake here, so I apologise if you waste your time on me.
You have problems with ~ symbol. In your os.execute("cd ~/testdir") is the shell who interprets the symbol and replaces it by your home path. However, in io.open("~/testdir/configfile.cfg", "w") is Lua who receives the string and Lua doesn't interprets this symbol, so your program tries to open a file in the incorrect folder. One simple solution is to call os.getenv("HOME") and concatenate the path string with your file path:
configfile = io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w")
In order to improve error messages I suggests you to wrap io.open() using assert() function:
configfile = assert( io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w") )

How to convert PIL image file into string in python3.4?

I have been trying to read a jpeg file using PIL in python 3.4. I need to save this file into string format. Although some options are provided on this site but I have tried a few but it is not working. Following is my code snippet which i have found on this site only:-
from io import StringIO
fp = Image.open("images/login.jpg")
output = StringIO()
fp.save(output, format="JPEG")
contents = output.getvalue()
output.close()
But i am facing the following error :-
TypeError: string argument expected, got 'bytes'
Could you please suggest what I have done wrong and how to get this working?
In python 3 you should use a BytesIO,
whereas as read in python docs:
StringIO is a native in-memory unicode container
.
Thanks a lot for the hint. I Actually have a found a different way of reading the image file and storing in string object in python2.x . Here is the code. Please let me know if there is any disadvantage of using this.
imgText = open("images/login.jpg", 'rb')
imgTextStr = imgText.read()
imgText.close()

Ruby require_relative not loading file, not throwing error

I am having trouble getting constant definitions loaded via an external file. I have narrowed the problem down to the following.
require_relative '../../common/config.rb'
A_CONSTANT = 'something'
puts "A_CONSTANT: #{A_CONSTANT}"
When I run this as written, it prints the message correctly. The same constant is declared in the file common/config.rb. The relative path is correct for the location of this file. Just for completeness, the above code is in /watir/dashboard/spec/ex.rb. The constant is declared in /watir/common/config.rb.
As I see it, the above code should error out for a duplicate constant declaration. It does not. If I comment out the constant declaration above and rerun, the puts statement shows an error for 'uninitialized constant.' Any ideas what's wrong?
Edit - The contents of the file common/config.rb are below.
A_CONSTANT = 'something'
On a lark, I changed the filename to common/conf.rb. When I modify the require_relative statement to load the renamed file, I get the results I originally expected. The file is loaded and the second constant declaration throws a warning saying 'already initialized constant.' If I comment out the second declaration, the script runs perfectly.
It appears that the filename 'config.rb' is somehow special when loaded by a relative path. I have use that filename in other scripts where it was in the same folder as the loading script or a sub-folder. This is the first time I have had to move up the tree to load it.
Ruby allows redefining constants, and will only print a warning. Some setting in your Ruby is just hiding that warning from you.

How to define default syntax on files without extension on Sublime Text 2?

I've read many posts dealing with this problem, but none has an answer to my question.
As said in the title, I would like to define a default syntax for all files which have no extension. In my case I would like to use the Shell syntax.
I've tried "View/Syntax/Open all with current extension as..." but for all files, I have to make again the manipulation.
I've tried the package "applySyntax" but it not seem to work with this configuration:
{
"name": "ShellScript/Shell-Unix-Generic",
"rules": [
{"file_name": "PRE_*$"}
]
}
All my files start with "PRE_[something]", someone know how to resolve this problem?
Thx!
I've found a Gist with a plugin to set the syntax based on the file name, I've modified it a bit to match files starting with PRE_:
import sublime_plugin
import os
class DetectFileTypeCommand(sublime_plugin.EventListener):
def on_load(self, view):
filename = view.file_name()
if not filename: # buffer has never been saved
return
name = os.path.basename(filename)
if name.startswith("PRE_"):
set_syntax(view, "Shell-Unix-Generic", "ShellScript")
def set_syntax(view, syntax, path=None):
if path is None:
path = syntax
view.settings().set('syntax', 'Packages/'+ path + '/' + syntax + '.tmLanguage')
print "Switched syntax to: " + syntax
You can go to Preferences->Browse Packages, and save it there ending with .py, I recommend creating a directory for it (e.g. DetectFileType/detect_file_type.py).

Resources