AttributeError: 'str' object has no attribute 'localtime' - python-3.x

I usually have sandbox where I try snippets of code out.
When I run the following code in my sandbox it works as expected
tuple_current_time = time.localtime(time.time())
print("tuple_current_time = ", tuple_current_time)
But when I run it in the module I am developing I get the error
AttributeError: 'str' object has no attribute 'localtime'
if not (sched_time) == "TIME":
print("Its not equal", sched_time)
#print ("Variable Type : ", type (tuple_current_time))
#Create a tuple of current time
tuple_current_time = time.localtime(time.time())
print("tuple_current_time = ", tuple_current_time)
tuple_website = newtuple=(tuple_current_time[0], tuple_current_time[1], tuple_current_time[2], sched_hour, sched_min, 0, 0, 0, 0)
print("tuple_website = ", tuple_website)
if tuple_website > tuple_current_time:
#print("Website tuple is newer than current time tuple")
else:
#print("Current time tuple is newer than website time tuple")
Please can anybody advise what I am doing wrong?
Thanks

Related

Folium : type object 'Map' has no attribute 'FeatureGroup'

Getting
Output as error : AttributeError: type object 'Map' has no attribute 'FeatureGroup'
When I run this
can_map = folium.Map(location=[56.130, -106.35],zoom_start=4,tiles='Stamen Toner')
ontario = folium.Map.FeatureGroup()
ontario.add_child(folium.features.CircleMarker([51.25, -85.32],radius=5,color='red', fill_color='Red'))
can_map.add_child(ontario)
folium.marker([51.25, -85,32],popup='Ontario').add_to(can_map)
can_map
FeatureGroup is not an attribute of Map. Should be folium.FeatureGroup(...)
I made some other fixes that are noted in the comments
can_map = folium.Map(location=[56.130, -106.35],zoom_start=4,tiles='Stamen Toner')
# changed folium.Map.FeatureGroup() to folium.FeatureGroup()
ontario = folium.FeatureGroup()
# changed folium.features.CircleMarker to folium.CircleMarker
ontario.add_child(folium.CircleMarker([51.25, -85.32],radius=5,color='red', fill_color='Red'))
can_map.add_child(ontario)
# changed folium.marker to folium.Marker
# changed -85,32 to 85.32
folium.Marker([51.25, -85.32],popup='Ontario').add_to(can_map)
can_map

Unable to delete Snapshot using boto3 using python3

I using boto3 on the python3 for delete the snapshop, Getting below error while trying to remove it (This syntax was work in the python2+boto only):
Tracebak (most recent call last):
File "./snapshotcleanup.py"m line 158, in <module>
s.delete()
AttributeError: 'dict' object has no attribute 'delete'
Code :
connection = myinternalclient (User, pass)
// Custom function for connection, you may consider ec2 = boto3.client('ec2')
res = connection.describe_snapshots(OwnersIds=[XX], Filters=[{'Name' : 'tag:Name', 'Value' : ["nonimp*"]'}])
for s in res['Snapshots']:
for tag in s['Tags']:
if 'nonprod' in tag.value():
s.delete()
print("[Deleted Snapshot]: %s" % s['SnapshotId'])
Is this syntax not in the boto3 ?
To delete the snapshot, you can use delete_snapshot method.
For example:
ec2 = boto3.client('ec2')
for s in res['Snapshots']:
for tag in s['Tags']:
if tag['Value'] == 'nonprod':
ec2.delete_snapshot(SnapshotId=s['SnapshotId'])
print("[Deleted Snapshot]: %s" % s['SnapshotId'])
Please double check the code as mistakes are possible, as one can delete wrong snapshots by accident.
The above assumes that the tags have the form (Key is not checked in the code above):
{
'Key': 'env',
'Value': 'nonprod'
}

Google Matrix API - python return Nonetype error

"Update"
*Finally resolved the issue, changed the try except to include TypeError and also use pass instead of continue in the except.
"End of update"
I wrote code to search for distance between two locations using Google Distance Matrix API. The origin location are fixed, however for the destination, I get it from an xlsx file. I was expecting to get Dictionary with Destination as the Key and the distance as value. When I run the code below, after certain loop I'm stumbled with this error code:
TypeError: Expected a lat/lng dict or tuple, but got NoneType
Can you help me understand the cause of the error? Here is the code (pygmap.py):
import googlemaps
import openpyxl
#get origin and destination locations
def cleanWB(file_path):
destination = list()
wb = openpyxl.load_workbook(filename=file_path)
ws = wb.get_sheet_by_name('Sheet1')
for i in range(ws.max_row):
cellValueLocation = ws.cell(row=i+2,column=1).value
destination.append(cellValueLocation)
#remove duplicates from destination list
unique_location = list(set(destination))
return unique_location
def getDistance(origin, destination):
#Google distance matrix API key
gmaps = googlemaps.Client(key = 'INSERT API KEY')
distance = gmaps.distance_matrix(origin, destination)
distance_status = distance['rows'][0]['elements'][0]['status']
if distance_status != 'ZERO_RESULTS':
jDistance = distance['rows'][0]['elements'][0]
distance_location = jDistance['distance']['value']
else:
distance_location = 0
return distance_location
And I run it using this code:
import pygmap
unique_location = pygmap.cleanWB('C:/Users/an_id/Documents/location.xlsx')
origin = 'alam sutera'
result = {}
for i in range(len(unique_location)):
try:
result[unique_location[i]] = pygmap.getDistance(origin, unique_location[i])
except (KeyError, TypeError):
pass
If I print results it will show that I have successfully get 46 results
result
{'Pondok Pinang': 25905, 'Jatinegara Kaum': 40453, 'Serdang': 1623167, 'Jatiasih
': 44737, 'Tanah Sereal': 77874, 'Jatikarya': 48399, 'Duri Kepa': 20716, 'Mampan
g Prapatan': 31880, 'Pondok Pucung': 12592, 'Johar Baru': 46791, 'Karet': 26889,
'Bukit Duri': 34039, 'Sukamaju': 55333, 'Pasir Gunung Selatan': 42140, 'Pinangs
ia': 30471, 'Pinang Ranti': 38099, 'Bantar Gebang': 50778, 'Sukabumi Utara': 204
41, 'Kembangan Utara': 17708, 'Kwitang': 25860, 'Kuningan Barat': 31231, 'Cilodo
ng': 58879, 'Pademangan Barat': 32585, 'Kebon Kelapa': 23452, 'Mekar Jaya': 5381
0, 'Kampung Bali': 1188894, 'Pajang': 30008, 'Sukamaju Baru': 53708, 'Benda Baru
': 19965, 'Sukabumi Selatan': 19095, 'Gandaria Utara': 28429, 'Setia Mulya': 635
34, 'Rawajati': 31724, 'Cireundeu': 28220, 'Cimuning': 55712, 'Lebak Bulus': 273
61, 'Kayuringin Jaya': 47560, 'Kedaung Kali Angke': 19171, 'Pagedangan': 16791,
'Karang Anyar': 171165, 'Petukangan Selatan': 18959, 'Rawabadak Selatan': 42765,
'Bojong Sari Baru': 26978, 'Padurenan': 53216, 'Jati Mekar': 2594703, 'Jatirang
ga': 51119}
Resolved the issue to include TypeError in the Try Except. And also use pass instead of continue
import pygmap
unique_location = pygmap.cleanWB('C:/Users/an_id/Documents/location.xlsx')
origin = 'alam sutera'
result = {}
#get getPlace
for i in range(len(unique_location)):
try:
result[unique_location[i]] = pygmap.getDistance(origin, unique_location[i])
except (KeyError, TypeError):
pass
I skipped some locations using this solution though.

How to display the text in SDL2.ext?

How to work with the text in the SOM 2. There is a code. I do not understand what went wrong.
bmpfont = sdl2.ext.BitmapFont(surface, (width, height))
software_renderer = sdl2.ext.SoftwareSpriteRenderSystem(window)
factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
sprite = factory.create_software_sprite((40, 40))
surf = bmpfont.render("4").surface
sprite.surface = surf
software_renderer.render(sprite, 0, 0)
gives an error message:
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected LP_SDL_Surface instance instead of SoftwareSprite
Exception ignored in:
LP_ is a ctypes indicator for long pointers. You'll need to call the contents method on the LP object to get the object you want.

Why can't I directly assign to object properties using multiple return values from a method?

Based on this question I've coded the following which throws a compilation time error:
Here is the code:
43. Currency currency = new Currency()
44. (currency.rate_one, currency.time_one) = getDateAndRate()
My method with two return values:
def getDateAndRate(){
Date date = new Date()
double rate = getRate();
return [rate, date]
}
Error thrown
expecting '}', found ',' # line 44, column 26.
(currency.rate_one, currency.time_one) = getDateAndRate()
^
Try this instead
def (rate, time) = getDateAndRate()
currency.rate_one = rate
currency.time_one = time
There is a trick I learned only recently myself and that is to combine multiple assignment and with:
with (currency) {
(rate_one, time_one) = getDateAndTime()
}

Resources