I´m building PDF document in python with fpdf. Fonts are set in the following way:
pdf.set_font('arial', 'B', 20)
However, this seems a bit unclear if I have to change the font several times (for headings, sub headings, regular text and so on). So I was trying to define fonts upfront in the following form:
heading = ["arial", "B", 20]
sub_heading = ["arial", "B", 15]
And then use the defined fonts later:
pdf.set_font(heading)
Unfortunately this results in the following error:
Traceback (most recent call last):
File "C:\Users\me\PycharmProjects\pythonProject\project\main.py", line 112, in <module>
pdf.set_font(heading)
File "C:\Users\me\PycharmProjects\pythonProject\project\venv\lib\site-packages\fpdf\fpdf.py", line 567, in set_font
family=family.lower()
AttributeError: 'list' object has no attribute 'lower'
So I was wondering, is there any other way to achieve this?
Try to use this:
pdf.set_font(*heading)
Related
i need to add to the beginning of a nested dictionary. it looks like move_to_end() is the easiest way for us to accomplish this but it seems that i cannot use this in a nested dictionary.
dict = OrderedDict({
'abdomen' : {"liver":3 , "spleen":1},
})
dict['abdomen'].update({'stomach':'2'})
dict['abdomen'].move_to_end('stomach', last = False)
print(dict['abdomen'])
generates the error:
Traceback (most recent call last):
File "test.py", line 232, in
dict['abdomen'].move_to_end('stomach', last = False)
AttributeError: 'dict' object has no attribute 'move_to_end'
The inner dictionary must be an OrderedDict. Change to the following:
my_dict = OrderedDict({
'abdomen': OrderedDict({"liver": 3, "spleen": 1}),
})
Note: Using built-in names (e.g., dict) is a bad idea. Change dict to something suitable.
I'm trying to build a program that will pick a random items (just once for each) from a list, that was imported from a file.
NB: I put only one item for a line in my file (no space, no coma, nothing else than a simple word)
I have a code like that for now:
file = open('file.txt', 'r')
myList = file.readlines()
myList[0]
rand_item = random.choice(myList)
print (rand_item)
I am just at the beginning of my program, so I'm just testing every new step that i make. Here, I'd like to display a random item from my list (itsefl imported from a file).
I have this message when I try to run my program:
Traceback (most recent call last):
File "C:/Users/Julien/Desktop/test final.py", line 16, in <module>
rand_item = random.choice(listEmployee)
AttributeError: 'builtin_function_or_method' object has no attribute 'choice'
I'm new to folium. I was able to produce the map I wanted from a dataframe.
df_map = folium.Map(location=[37.750999450684, -97.821998596191], zoom_start=4)
for each in df[0:len(df)].iterrows():
folium.CircleMarker(location=[each[1]["GEO_LAT_0"], each[1]["GEO_LNG_0"]],
radius=5.0, color='#3186cc', fill_color='#3186cc').add_to(df_map)
Map comes out nicely.
Next I wanted to add popups from a third column in data frame, and can't seem to get syntax right. Not clear how I might add these popups from folium documentation. An error message I can't interpret results from this code:
df_map = folium.Map(location=[37.750999450684, -97.821998596191], zoom_start=4)
for each in df[0:len(df)].iterrows():
folium.CircleMarker(location=[each[1]["GEO_LAT_0"], each[1]["GEO_LNG_0"]],
**popup=each[1]["GEO_CITY_0"],**
radius=5.0, color='#3186cc',fill_color='#3186cc').add_to(df_map)
To verify my loop and dataframe were ok, I substituted a
print each[1]["GEO_CITY_0"]
within the for-each loop instead of folium.circlemarker and it worked fine. Something is wrong when I use the popup syntax above.
AttributeError: 'float' object has no attribute 'get_name'
Your help appreciated. Thanks p.s. Full message is:
Traceback (most recent call last):
File "", line 4, in
radius=1, color='#3186cc', fill_color='#3186cc').add_to(df_map)
File "C:\Users\Peter\Anaconda3\lib\site-packages\folium\features.py", line 870, in init
super(CircleMarker, self).init(location=location, popup=popup)
File "C:\Users\Peter\Anaconda3\lib\site-packages\folium\map.py", line 652, in init
self.add_child(popup)
File "C:\Users\Peter\Anaconda3\lib\site-packages\branca\element.py", line 96, in add_child
name = child.get_name()
AttributeError: 'float' object has no attribute 'get_name'
I was under the impression that #pzajonc's syntax of popup=each[1]["GEO_CITY_0"] would work in recent versions (OP's has 0.4.0) of folium.
Here's a github issue that mentions the error and the fix
Regardless, changing it to popup=folium.Popup(each[1]["GEO_CITY_0"]) will resolve the issue.
I am learning how to create transducers with Pyfst and I am trying to visualize the ones I create. The ultimate goal is to be able to write the transducers to dot files and see them in Graphviz.
I took a sample code to see how to visualize the following acceptor.
a = fst.Acceptor()
a.add_arc(0, 1, 'x', 0.1)
a[1].final = -1
a.draw()
When I use draw(), which comes with the package, I get an error:
File "/Users/.../tests.py", line 42, in <module>
a.draw()
File "_fst.pyx", line 816, in fst._fst.StdVectorFst.draw
(fst/_fst.cpp:15487)
File "/Users/.../venv-3.6/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
If I try to write the above mentioned acceptor to .dot via this:
def fst_dot(dot_object, filename):
path, file = split(filename)
new_path = join(dot_files_folder_path, path)
if not os.path.exists(new_path):
os.makedirs(new_path)
if hasattr(dot_object, 'dotFormat'):
draw_string = dot_object.dotFormat()
else:
draw_string = dot_object.draw()
open(join(dot_files_folder_path, filename + ".dot"), "w").write(draw_string)
then also I get the following error:
File "/Users/...tests.py", line 43, in <module>
fst_dot(a, 'acceptor')
File "/Users/...tests.py", line 22, in fst_dot
draw_string = dot_object.draw()
File "_fst.pyx", line 816, in fst._fst.StdVectorFst.draw
(fst/_fst.cpp:15487)
File "/Users/.../venv-3.6/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
So, both errors look the same - there is some kind of an issue with draw().
On the pyfst site it says that draw is used for dot format representation of the transducer.
I can't understand how to fix the error. Any help would be greatly appreciated.
I am using OSX and PyCharm.
You might try using Python2 to see if that helps at all.
However, I think you'll be better off using the Python bindings that are included with OpenFST 1.5+. That library also has the ability to write to GraphViz .dot files. There is documentation available here:
http://www.openfst.org/twiki/bin/view/FST/PythonExtension
I recommend you fstdraw command from openfst.
after a.write('tmp.fst') in python.
$ fstdraw tmp.fst > tmp.dot in shell.
EDIT:
Finally, I found that UFAL's forked pyfst works fine with python3.
https://github.com/UFAL-DSG/pyfst
So I just started using python and pygame to start building a poker program, and I downloaded a bunch of images for the cards. I started making a test program for the images:
import pygame
pygame.init()
screen = pygame.display.set_mode((1200, 675)) #Sets the screen to a 16:9 ratio display
pygame.display.set_caption('Poker Program') #Used to set the window name as whatever's in the brackets
feltGreen = (0,200,0) #What is used for the color (r,g,b)(the higher the number, the lighter the colour)
screen.fill(feltGreen)
pygame.display.update() #Updates the screen
twoc = pygame.image("2_of_clubs.png")
twod = pygame.image("2_of_diamonds.png")
twoh = pygame.image("2_of_hearts.png")
twos = pygame.image("2_of_spades.png")
threec = pygame.image("3_of_clubs.png")
threed = pygame.image("3_of_diamonds.png")
threeh = pygame.image("3_of_hearts.png")
threes = pygame.image("3_of_spades.png")
For the first while, it works perfectly. But then I get this:
Traceback (most recent call last):
File "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py", line 15, in <module>
twoc = pygame.image("2_of_clubs.png")
TypeError: 'module' object is not callable
I've tried everything, but it still says that. Is there anything I can do to make it work?
PS The folder where I store the cards is in the scripts folder of Python35.
Update: I've replaced all the pygame.image with pygame.image.load, but now I get this instead:
Traceback (most recent call last):
File "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py", line 15, in <module>
twoc = pygame.image.load("2_of_clubs.png") #Lines 15-66 bring all the cards into the program
pygame.error: Couldn't open 2_of_clubs.png
The problem is that pygame.image is not actually a function - it's a module (hence the text of the error). To load an image, use the function pygame.image.load(filename).
pygame.image is not a function so is not callable. You probably want to use pygame.image.load() instead.
Source: http://www.pygame.org/docs/ref/image.html
Edit:
You're probably getting the new error because python can't find the file. You should use the absolute path of the file. E.g. pygame.image.load('C:\\pictures\\2_of_clubs.png').
Ps. you don't need to list all those modules in your question. It's making the question unnecessarily long.