I have the below code, and as someone new to Python, I was hoping for suggestions on making it more Pythonic. Posters in this thread have provided very helpful numpy and list comprehension solutions; however, I'd like to improve my more basic nested loop solution solely for the sake of becoming better with basic Python tasks (please do let me know if I should have posted this in the prior thread instead of starting a new semi-duplicate...apologies if that's what I should have done).
Here's the current code (which works as desired):
sales_data = [[201703, 'Bob', 3000], [201703, 'Sarah', 6000], [201703,
'Jim', 9000], [201704, 'Bob', 8000], [201704, 'Sarah', 7000], [201704,
'Jim', 12000], [201705, 'Bob', 15000], [201705, 'Sarah', 14000], [201705,
'Jim', 8000], [201706, 'Bob', 10000], [201706, 'Sarah', 18000]]
sorted_sales_data = sorted(sales_data, key=lambda x: -x[2])
date_list = []
sales_ranks = []
for i in sales_data:
date_list.append(i[0])
sorted_dates = sorted(set(date_list), reverse=True)
for i in sorted_dates:
tmp_lst = []
tmp_lst.append(i)
for j in sorted_sales_data:
if j[0] == i:
tmp_lst.append(j[1])
sales_ranks.append(tmp_lst)
print(sales_ranks)
Any suggestions on making this more Pythonic (other than numpy or list comprehension solutions) would be greatly appreciated. I am using Python 3.6.
Related
Hi I'm new to Tensorflow and I've been practicing with the tensorflow.estimator library. Basically I ran the inbuilt tf.estimator.DNNClassifier algorithm below
import tensorflow as tf
def train_input_fn(features, labels, batch_size):
"""An input function for training"""
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
# Shuffle, repeat, and batch the examples.
return dataset.shuffle(1000).repeat().batch(batch_size)
# Feature columns describe how to use the input.
my_feature_columns = []
for key in landmark_features.keys():
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
# Build a DNN with 2 hidden layers and 10 nodes in each hidden layer.
classifier = tf.estimator.DNNClassifier(feature_columns=my_feature_columns, hidden_units=[10, 10],n_classes=10)
dataset = train_input_fn(landmark_features, emotion_labels, batch_size = 1375 )
However I keep getting the following error:
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpc_tag0rc
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpc_tag0rc', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
rewrite_options {
meta_optimizer_iterations: ONE
}
}
Any idea on what I can do to fix my code ?
I am working on a project that will hopefully allow me to combine the Watson Python SDK implementation of speech-to-text...Watson Conversation...and text-to-speech. I am running into some problems though working with the Python 2.7 json data. I am actually trying to do two things:
1) I want to parse the json data just for the transcript values and it would be awesome if I could combine those values into an easily readable string format for use later in the program.
2) The other thing I need to do is manipulate the json in a way that would allow me to use it as input for the conversation or text-to-speech sections. Basically, how can I convert whats provided in the json into acceptable input for the other Watson modules?
What I've tried so far:
I read the Python 2.7 json docs and tried to convert it back into a Python dictionary which sort of worked? All of the key:value pairs had a "u" before them and none of the regular dictionary methods seemed to work on them. Also, they don't look like the standard Key:Value combinations. I was able to put all of the json data in one variable though. I'll post my code below (ignore the print statements as I was just checking to see how the data looked at each step), but it's mostly just what you can get from the github examples section.
** Just a quick final question too: Is the Python SDK limited in any way compared to the other ones (Java, JScript, etc) because it seems like their output is much easier to work with?
import json
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
speech_to_text = SpeechToTextV1(
username='###########',
password='###########,
x_watson_learning_opt_out=True
)
with open(join(dirname(__file__), '/home/user/Desktop/output.wav'),'rb') as audio_file:
json_str = (json.dumps(speech_to_text.recognize(audio_file, content_type='audio/wav', timestamps=False, word_confidence=False,
model='en-US_NarrowbandModel'), indent=2))
print json_str
json_dict = json.loads(json_str)
print json_dict
def main(args):
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
The issue appears to me that you are dumping your JSON to a string, then trying to access it as an object.
Using the following sample code, it works.
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
speech_to_text = SpeechToTextV1(
username='....',
password='....',
x_watson_learning_opt_out=True
)
with open('../blog/ihaveadream.wav','rb') as audio_file:
response = speech_to_text.recognize(audio_file, content_type='audio/wav', timestamps=False, word_confidence=False, model='en-US_NarrowbandModel')
print json.dumps(response, indent=2)
This returns the following:
{
"results": [
{
"alternatives": [
{
"confidence": 1.0,
"transcript": "I still have a dream "
}
],
"final": true
},
{
"alternatives": [
{
"confidence": 0.999,
"transcript": "it is a dream deeply rooted in the American dream I have a dream "
}
],
"final": true
},
{
"alternatives": [
{
"confidence": 1.0,
"transcript": "that one day this nation will rise up and live out the true meaning of its creed we hold these truths to be self evident that all men are created equal "
}
],
"final": true
}
],
"result_index": 0,
"warnings": [
"Unknown arguments: continuous."
]
}
So if you wanted to access the top level response you can do the following.
print 'Confidence: {}'.format(response['results'][0]['alternatives'][0]['confidence'])
print 'Transcript: {}'.format(response['results'][0]['alternatives'][0]['transcript'])
The output of that would be:
Confidence: 1.0
Transcript: I still have a dream
Trying to figure out why I can't get Lambda to work with writing a file.
Here is my code:
list = (i*2 for i in range(10))
file = open("text.txt", "a")
lambda i: (file.write(str(i) + "\n")), list
and I don't even get to close the file before I get following error message:
(<function <lambda> at 0x021E1B30>, <generator object <genexpr> at 0x021EC418>)
Based on your comment, I guess what you're trying to do is:
map(lambda i: file.write(str(i) + "\n"), list)
However, I think the code you were apparently trying to avoid would be more readable:
for i in list:
file.write(str(i) + "\n")
(Also, take Peter's note to heart... using list as a variable will come back to haunt you someday when you try to use the list() built-in function and find it's been overridden.)
I'm trying to implement a simple sub dowloader with Selenium on Python 2.7 going on the http://www.yifysubtitles.com website.
I have run into serious troubles with implementing waits either implicit or explicit in Selenium - surely caused by ignorance rather than code.
I eventually opted for the destroy-all time.sleep() solution. The program works but I would like a more programatic solution and a prettier code.
Here is the code with the both implicit and explicit waits:
user_input = raw_input("Enter the movie of interest: ")
profile = SetProfile()
browser = webdriver.Firefox(profile)
# browser.implicitly_wait(30)
WindowSize(400, 400)
browser.get(url)
print "Searching..."
search_bar = browser.find_element_by_id("qSearch")
click_button = browser.find_element_by_class_name("glyphicon")
#wait_1 = WebDriverWait(browser, 50)
#cookie_element = wait_1.until(EC.element_to_be_clickable((By.CLASS_NAME,"cc_btn.cc_btn_accept_all")))
time.sleep(5)
accept_cookies_btn = browser.find_element_by_class_name("cc_btn.cc_btn_accept_all")
search_bar.clear()
accept_cookies_btn.click()
search_bar.send_keys(user_input)
search_bar.send_keys(Keys.RETURN)
time.sleep(10)
#wait_2 = WebDriverWait(browser, 50)
#result_element = wait_2.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.media-heading")))
result = browser.find_element_by_class_name("media-heading")
result.click()
print "Movie found!"
time.sleep(10)
req = requests.get(browser.current_url)
soup = BeautifulSoup(req.text)
link_to_sub = ResultParser(soup)
print "Choosing the best eng. subtitle..."
browser.get(url+link_to_sub[1])
download_button = browser.find_element_by_class_name("btn-icon")
download_button.click()
time.sleep(10)
browser.quit()
print "Enjoy!"
I have commented out the waits (explicit or implicit) I tried to use. Indeed, If I use waits instead time.sleep, Selenium will throw at me an ElementNotVisibleException.
Thus, am I doing something wrong with the waits?
Regarding the implicit waits, Selenium always throws at me ElementNotVisibleException: Message: (With message empty) when I run the program, whatever the wait length (tried 5, 30 and 100!).
When I use explicit waits, similarly, Selenium either throw ElementNotVisibleException or seems to use the DOM from the preceding page. Indeed, in the last case the wait is after clicking on the result, but I get the heading of the previous page...
As I said, when I use time.sleep(), Selenium finds the elements without any trouble.
A long message for a simple question: why my waits seem to not work?
I am using Django + mongoengine
I want to update the books (embedded document) count in my Bookscollection document. I want the update query to return the full object. Therefore I am using the 'find_and_modify'. But even if I use update or remove fields. I still get an error that 'Must either update or remove'.
Here is my code snippet -
for book_id in book_list:
book_col = BookCollection._get_collection().find_and_modify({
'query': {'coll_id':book_coll_id,'book.book_id':book_id},
'update':{'$inc':
{'book.$.quantity' : 1}
},
'new':True
})
What is wrong here?
find_and_modify takes a keywords arguments that is why you are getting that error message.
for book_id in book_list:
book_col = BookCollection._get_collection().find_and_modify({
query={'coll_id':book_coll_id,'book.book_id': book_id},
update={'$inc': {'book.$.quantity' : 1}},
new=True
})
Also you should know that find_and_modify is deprecated in pymongo3.0 you should use the find_one_and_update if your diver is pymongo 2.9 or newer.
BookCollection._get_collection().find_and_modify(
query={'coll_id':book_coll_id,'book.book_id': book_id},
update={'$inc': {'book.$.quantity' : 1}},
new=True
)
For anyone coming here after pymongo 3.0, you need to use .find_one_and_update instead of find_and_modify since it is deprecated.
example:
book_col = BookCollection._get_collection().find_one_and_update(
filter={'coll_id':book_coll_id,'book.book_id': book_id},
update={'$inc': {'book.$.quantity' : 1}},
new=True
)