diff --git "a/Output.txt" "b/Output.txt" new file mode 100644--- /dev/null +++ "b/Output.txt" @@ -0,0 +1,5049 @@ +NL: send a signal `signal.sigusr1` to the current process +ref: os.kill(os.getpid(), signal.SIGUSR1) +Output: time.send('signal.si/r1', signal.SIGKILL) +bleu_metric: 17.694975149532556 +---------- +NL: decode a hex string '4a4b4c' to utf-8. +ref: bytes.fromhex('4a4b4c').decode('utf-8') +Output: binascii.unhexlify('4A4B4c', 16) +bleu_metric: 10.851293189170718 +---------- +NL: check if all elements in list `mylist` are identical +ref: all(x == myList[0] for x in myList) +Output: all(isinstance(x, int) for x in mylist) +bleu_metric: 19.340017482720636 +---------- +NL: format number of spaces between strings `python`, `:` and `very good` to be `20` +ref: print('%*s : %*s' % (20, 'Python', 20, 'Very Good')) +Output: """{:20}""".format(your_string, your_string) +bleu_metric: 7.614627140286717 +---------- +NL: how to convert a string from cp-1251 to utf-8? +ref: d.decode('cp1251').encode('utf8') +Output: cp1251.decode('utf-8').encode('utf-8') +bleu_metric: 46.298522813477696 +---------- +NL: get rid of none values in dictionary `kwargs` +ref: res = {k: v for k, v in list(kwargs.items()) if v is not None} +Output: ▁dict((kwargs.values()) for kwargs in▁list(kwargs.items() +bleu_metric: 29.736677967239018 +---------- +NL: get rid of none values in dictionary `kwargs` +ref: res = dict((k, v) for k, v in kwargs.items() if v is not None) +Output: ▁dict((kwargs.values()) for kwargs in▁list(kwargs.items() +bleu_metric: 25.752718543785992 +---------- +NL: capture final output of a chain of system commands `ps -ef | grep something | wc -l` +ref: subprocess.check_output('ps -ef | grep something | wc -l', shell=True) +Output: time.sleep(1) +bleu_metric: 3.2586794879778176 +---------- +NL: concatenate a list of strings `['a', 'b', 'c']` +ref: """""".join(['a', 'b', 'c']) +Output: ['a', 'b', 'c'] +bleu_metric: 45.715029516916914 +---------- +NL: find intersection data between series `s1` and series `s2` +ref: pd.Series(list(set(s1).intersection(set(s2)))) +Output: s1.intersection(s1, s2.intersection(s2)) +bleu_metric: 24.554859518133163 +---------- +NL: sending http headers to `client` +ref: client.send('HTTP/1.0 200 OK\r\n') +Output: client.send('http://www.google.com') +bleu_metric: 31.482474520658833 +---------- +NL: format a datetime string `when` to extract date only +ref: then = datetime.datetime.strptime(when, '%Y-%m-%d').date() +Output: ▁datetime.datetime.strptime('January 11, 2010', '%Y-%m-%d').date() +bleu_metric: 68.50564735741162 +---------- +NL: split a multi-line string `inputstring` into separate strings +ref: inputString.split('\n') +Output: inputstring.split(',') +bleu_metric: 42.06930013115334 +---------- +NL: split a multi-line string ` a \n b \r\n c ` by new line character `\n` +ref: ' a \n b \r\n c '.split('\n') +Output: a.rstrip().split('\n') +bleu_metric: 38.90205926838098 +---------- +NL: concatenate elements of list `b` by a colon ":" +ref: """:""".join(str(x) for x in b) +Output: b = [(x + y) for x, y in zip(a, b)] +bleu_metric: 15.821285888349262 +---------- +NL: get the first object from a queryset in django model `entry` +ref: Entry.objects.filter()[:1].get() +Output: ▁Entry.objects.filter(id=1).order_by('id')[0] +bleu_metric: 30.123786461877337 +---------- +NL: calculate sum over all rows of 2d numpy array +ref: a.sum(axis=1) +Output: np.sum(a,▁axis=0) +bleu_metric: 26.91109110344471 +---------- +NL: enable warnings using action 'always' +ref: warnings.simplefilter('always') +Output: ▁warnings.warn('always') +bleu_metric: 57.06745777055999 +---------- +NL: concatenate items of list `l` with a space ' ' +ref: print(' '.join(map(str, l))) +Output: [' ','','',''.join(map(str, l)) for l in zip(l +bleu_metric: 41.653935052504146 +---------- +NL: run script 'hello.py' with argument 'htmlfilename.htm' on terminal using python executable +ref: subprocess.call(['python.exe', 'hello.py', 'htmlfilename.htm']) +Output: subprocess.call(['hello.py', 'htmlfilename.htm']) +bleu_metric: 69.67212594723009 +---------- +NL: how can i parse a time string containing milliseconds in it with python? +ref: time.strptime('30/03/09 16:31:32.123', '%d/%m/%y %H:%M:%S.%f') +Output: ▁datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%M:% +bleu_metric: 23.516840244143285 +---------- +NL: convert a string `my_string` with dot and comma into a float number `my_float` +ref: my_float = float(my_string.replace(',', '')) +Output: ▁float(my_string.split(',')) +bleu_metric: 45.67575584464341 +---------- +NL: convert a string `123,456.908` with dot and comma into a floating number +ref: float('123,456.908'.replace(',', '')) +Output: ▁struct.unpack('(?<=.)', '123,45s.908)[2:]) +bleu_metric: 15.85266710543935 +---------- +NL: set pythonpath in python script. +ref: sys.path.append('/path/to/whatever') +Output: os.path.dirname(os.path.abspath(__file__)) +bleu_metric: 18.295654224495205 +---------- +NL: split string 'words, words, words.' using a regex '(\\w+)' +ref: re.split('(\\W+)', 'Words, words, words.') +Output: re.split('(\\w+)', 'Words,▁words,▁words.') +bleu_metric: 68.42666550297749 +---------- +NL: open a file `output.txt` in append mode +ref: file = open('Output.txt', 'a') +Output: output.txt(open('output.txt', 'r')) +bleu_metric: 36.327039079325615 +---------- +NL: download a file "http://www.example.com/songs/mp3.mp3" over http and save to "mp3.mp3" +ref: urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3') +Output: ▁urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', +bleu_metric: 77.20315796580198 +---------- +NL: download a file `url` over http and save to `file_name` +ref: u = urllib.request.urlopen(url) +Output: ▁urllib.request.urlretrieve('http://www.example.com/',▁filename) +bleu_metric: 16.081986819293686 +---------- +NL: download a file 'http://www.example.com/' over http +ref: response = urllib.request.urlopen('http://www.example.com/') +html = response.read() +Output: ▁urllib.request.urlretrieve('http://www.example.com/') +bleu_metric: 55.1505335495472 +---------- +NL: download a file `url` over http +ref: r = requests.get(url) +Output: ▁urllib.request.urlretrieve(url) +bleu_metric: 33.18077402843942 +---------- +NL: download a file `url` over http and save to "10mb" +ref: response = requests.get(url, stream=True) +Output: ▁urllib.request.urlretrieve('http://www.google.com/') +bleu_metric: 8.282282660969601 +---------- +NL: argparse add argument with flag '--version' and version action of '%(prog)s 2.0' to parser `parser` +ref: parser.add_argument('--version', action='version', version='%(prog)s 2.0') +Output: parser.add_argument('---version', parser='%(prog)s▁2.0') +bleu_metric: 63.536186277548154 +---------- +NL: remove key 'c' from dictionary `d` +ref: {i: d[i] for i in d if i != 'c'} +Output: d =▁dict((k, v) for k, v in d.items()▁if k▁!= 'c') +bleu_metric: 17.787737198601235 +---------- +NL: create new dataframe object by merging columns "key" of dataframes `split_df` and `csv_df` and rename the columns from dataframes `split_df` and `csv_df` with suffix `_left` and `_right` respectively +ref: pd.merge(split_df, csv_df, on=['key'], suffixes=('_left', '_right')) +Output: df = pd.DataFrame(['key','split_df', 'csv_df' +bleu_metric: 18.454987996483485 +---------- +NL: split a string `s` by space with `4` splits +ref: s.split(' ', 4) +Output: s.split(4) +bleu_metric: 45.043204423208614 +---------- +NL: read keyboard-input +ref: input('Enter your input:') +Output: input() +bleu_metric: 13.267398701010466 +---------- +NL: enable debug mode on flask application `app` +ref: app.run(debug=True) +Output: ▁app.run(debug=True) +bleu_metric: 80.70557274927982 +---------- +NL: python save list `mylist` to file object 'save.txt' +ref: pickle.dump(mylist, open('save.txt', 'wb')) +Output: mylist.save('save.txt') +bleu_metric: 27.374248660963534 +---------- +NL: multiply a matrix `p` with a 3d tensor `t` in scipy +ref: scipy.tensordot(P, T, axes=[1, 1]).swapaxes(0, 1) +Output: np.dot(tf, tf, tf) +bleu_metric: 5.6829100730876085 +---------- +NL: create 3d array of zeroes of size `(3,3,3)` +ref: numpy.zeros((3, 3, 3)) +Output: np.zeros((3, 3, 3)) +bleu_metric: 91.93227152249185 +---------- +NL: cut off the last word of a sentence `content` +ref: """ """.join(content.split(' ')[:-1]) +Output: re.split('\\.\\s', text) +bleu_metric: 15.903644176951381 +---------- +NL: convert scalar `x` to array +ref: x = np.asarray(x).reshape(1, -1)[(0), :] +Output: x = np.array([(x) for x in range(10)]) +bleu_metric: 22.64081604016773 +---------- +NL: sum all elements of nested list `l` +ref: sum(sum(i) if isinstance(i, list) else i for i in L) +Output: ▁sum(i for i, j in enumerate(l)▁if j == j) +bleu_metric: 18.931747781986427 +---------- +NL: convert hex string '470fc614' to a float number +ref: struct.unpack('!f', '470FC614'.decode('hex'))[0] +Output: ▁struct.unpack('470fc614', 16)[0] +bleu_metric: 32.22245000438681 +---------- +NL: multiple each value by `2` for all keys in a dictionary `my_dict` +ref: my_dict.update((x, y * 2) for x, y in list(my_dict.items())) +Output: ▁dict((k, v) for k, v in my_dict.items()▁if v == 2) +bleu_metric: 24.351580099223153 +---------- +NL: running bash script 'sleep.sh' +ref: subprocess.call('sleep.sh', shell=True) +Output: os.system('sleep.sh') +bleu_metric: 40.728895472895154 +---------- +NL: join elements of list `l` with a comma `,` +ref: """,""".join(l) +Output: [(x + y) for x, y in zip(l, l[1:])] +bleu_metric: 8.24055698798382 +---------- +NL: make a comma-separated string from a list `mylist` +ref: myList = ','.join(map(str, myList)) +Output: mylist =▁map(int, mylist.split(',')) +bleu_metric: 22.401343373458893 +---------- +NL: reverse the list that contains 1 to 10 +ref: list(reversed(list(range(10)))) +Output: ▁list(reversed([1, 10])) +bleu_metric: 33.88714363186177 +---------- +NL: remove substring 'bag,' from a string 'lamp, bag, mirror' +ref: print('lamp, bag, mirror'.replace('bag,', '')) +Output: re.sub('(?<=[a-z])([A-Z])', '\\1', ' +bleu_metric: 10.926857823700987 +---------- +NL: reverse the order of words, delimited by `.`, in string `s` +ref: """.""".join(s.split('.')[::-1]) +Output: s.split(',')[::-1] +bleu_metric: 34.047433481921914 +---------- +NL: convert epoch time represented as milliseconds `s` to string using format '%y-%m-%d %h:%m:%s.%f' +ref: datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f') +Output: time.strftime('%Y-%m-%d %H:%M:%S.%f') +bleu_metric: 74.24520947691984 +---------- +NL: parse milliseconds epoch time '1236472051807' to format '%y-%m-%d %h:%m:%s' +ref: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1236472051807 / 1000.0)) +Output: ▁datetime.datetime.strptime('1236472051807', '%Y-%m-%d %H: +bleu_metric: 36.283299841087896 +---------- +NL: get the date 7 days before the current date +ref: (datetime.datetime.now() - datetime.timedelta(days=7)).date() +Output: ▁datetime.datetime.now().date() +bleu_metric: 39.17901378181805 +---------- +NL: sum elements at index `column` of each list in list `data` +ref: print(sum(row[column] for row in data)) +Output: [sum(x) for x in data] +bleu_metric: 15.924924144387933 +---------- +NL: sum columns of a list `array` +ref: [sum(row[i] for row in array) for i in range(len(array[0]))] +Output: ▁sum(df.sum(axis=1)) +bleu_metric: 6.052326593839414 +---------- +NL: encode binary string 'your string' to base64 code +ref: base64.b64encode(bytes('your string', 'utf-8')) +Output: base64.b64encode('your▁string') +bleu_metric: 24.554248848222283 +---------- +NL: combine list of dictionaries `dicts` with the same keys in each list to a single dictionary +ref: dict((k, [d[k] for d in dicts]) for k in dicts[0]) +Output: ▁dict((k, v) for d in▁dicts for k, v in▁list(d.items())) +bleu_metric: 24.10600823901789 +---------- +NL: merge a nested dictionary `dicts` into a flat dictionary by concatenating nested values with the same key `k` +ref: {k: [d[k] for d in dicts] for k in dicts[0]} +Output: ▁dict((k, v) for k, v in▁list(dicts.items())) +bleu_metric: 8.17178415827399 +---------- +NL: how do i get the url parameter in a flask view +ref: request.args['myParam'] +Output: ▁app.run(debug=True) +bleu_metric: 12.605968092174912 +---------- +NL: identify duplicate values in list `mylist` +ref: [k for k, v in list(Counter(mylist).items()) if v > 1] +Output: set(['a', 'b', 'c']) +bleu_metric: 6.171879938074473 +---------- +NL: insert directory 'apps' into directory `__file__` +ref: sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps')) +Output: ▁sys.path.insert(0, 'apps') +bleu_metric: 24.758945556587445 +---------- +NL: modify sys.path for python module `subdir` +ref: sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) +Output: ▁sys.path.append(subdir) +bleu_metric: 13.681283243087492 +---------- +NL: insert a 'none' value into a sqlite3 table. +ref: db.execute("INSERT INTO present VALUES('test2', ?, 10)", (None,)) +Output: db.commit() +bleu_metric: 1.3648974794270936 +---------- +NL: flatten list `list_of_menuitems` +ref: [image for menuitem in list_of_menuitems for image in menuitem] +Output: ▁list_of_enumerate(chain.from_iterable(list_of_menuitems)) +bleu_metric: 11.368272367804307 +---------- +NL: append elements of a set `b` to a list `a` +ref: a.extend(b) +Output: a.append(b) +bleu_metric: 59.46035575013605 +---------- +NL: append elements of a set to a list in python +ref: a.extend(list(b)) +Output: a = [[] for i in range(3)] +bleu_metric: 11.114924776032007 +---------- +NL: write the data of dataframe `df` into text file `np.txt` +ref: np.savetxt('c:\\data\\np.txt', df.values, fmt='%d') +Output: df.to_numeric(np.txt) +bleu_metric: 3.9614496879183108 +---------- +NL: write content of dataframe `df` into text file 'c:\\data\\pandas.txt' +ref: df.to_csv('c:\\data\\pandas.txt', header=None, index=None, sep=' ', mode='a') +Output: df.to_csv('c:\\data\\pandas.txt') +bleu_metric: 33.15109099076485 +---------- +NL: split a string `x` by last occurrence of character `-` +ref: print(x.rpartition('-')[0]) +Output: x.split('-', 1)[-1] +bleu_metric: 30.856960108358766 +---------- +NL: get the last part of a string before the character '-' +ref: print(x.rsplit('-', 1)[0]) +Output: a[-1:] + a[:-1] +bleu_metric: 9.389419352536649 +---------- +NL: upload file using ftp +ref: ftp.storlines('STOR ' + filename, open(filename, 'r')) +Output: ▁ftp.quit() +bleu_metric: 6.197705798903779 +---------- +NL: add one to the hidden web element with id 'xyz' with selenium python script +ref: browser.execute_script("document.getElementById('XYZ').value+='1'") +Output: ▁driver.execute_script('http://stackoverflow.com') +bleu_metric: 16.310495756291736 +---------- +NL: create array containing the maximum value of respective elements of array `[2, 3, 4]` and array `[1, 5, 2]` +ref: np.maximum([2, 3, 4], [1, 5, 2]) +Output: np.argmax([2, 3, 4], [1, 5, 2]) +bleu_metric: 88.67047947918724 +---------- +NL: print a list `l` and move first 3 elements to the end of the list +ref: print(l[3:] + l[:3]) +Output: l = [i for i, l in enumerate(l, 3)▁if l == 3] +bleu_metric: 10.414419091986518 +---------- +NL: loop over files in directory '.' +ref: for fn in os.listdir('.'): +Output: for item in os.listdir('.') in os.listdir('.'):▁pass +bleu_metric: 42.902556537105646 +---------- +NL: loop over files in directory `source` +ref: for (root, dirs, filenames) in os.walk(source): +Output: for (not▁file in os.walk(source)):▁pass +bleu_metric: 45.845403319927435 +---------- +NL: create a random list of integers +ref: [int(1000 * random.random()) for i in range(10000)] +Output: ▁randomList = [random.random() for _ in range(10)] +bleu_metric: 35.22146109090851 +---------- +NL: using %f with strftime() in python to get microseconds +ref: datetime.datetime.now().strftime('%H:%M:%S.%f') +Output: ▁print(strftime('%m/%d %H:%M:%S')) +bleu_metric: 45.02648619040337 +---------- +NL: google app engine execute gql query 'select * from schedule where station = $1' with parameter `foo.key()` +ref: db.GqlQuery('SELECT * FROM Schedule WHERE station = $1', foo.key()) +Output: ▁cursor.execute('SELECT *▁FROM▁table▁WHERE station = $1', (foo.key()) +bleu_metric: 50.76453300895118 +---------- +NL: filter rows in pandas starting with alphabet 'f' using regular expression. +ref: df.b.str.contains('^f') +Output: df[(df['a'] > 1) & (df['b'] > 1)] +bleu_metric: 6.871624004919695 +---------- +NL: print a 2 dimensional list `tab` as a table with delimiters +ref: print('\n'.join('\t'.join(str(col) for col in row) for row in tab)) +Output: a = [(a[i], b[i]) for i in range(0, len(a), 2)] +bleu_metric: 6.711116671457737 +---------- +NL: pandas: delete rows in dataframe `df` based on multiple columns values +ref: df.set_index(list('BC')).drop(tuples, errors='ignore').reset_index() +Output: df.loc[df.columns == [(df.columns▁!= 0).any(axis=1)] +bleu_metric: 8.722785633659552 +---------- +NL: format the variables `self.goals` and `self.penalties` using string formatting +ref: """({:d} goals, ${:d})""".format(self.goals, self.penalties) +Output: ▁print('{} {}'.format(self.goals,▁self.penalties)) +bleu_metric: 34.66537079680952 +---------- +NL: format string "({} goals, ${})" with variables `goals` and `penalties` +ref: """({} goals, ${})""".format(self.goals, self.penalties) +Output: """({}, ${})""".format(goals, cents) +bleu_metric: 61.17470797490066 +---------- +NL: format string "({0.goals} goals, ${0.penalties})" +ref: """({0.goals} goals, ${0.penalties})""".format(self) +Output: """({0.goals}, ${0.penalties})""".format('${0.go +bleu_metric: 74.24213297217366 +---------- +NL: convert list of lists `l` to list of integers +ref: [int(''.join(str(d) for d in x)) for x in L] +Output: [int(x) for x in L] +bleu_metric: 26.91716452870884 +---------- +NL: combine elements of each list in list `l` into digits of a single integer +ref: [''.join(str(d) for d in x) for x in L] +Output: [int(x) for x in L] +bleu_metric: 31.16211115352542 +---------- +NL: convert a list of lists `l` to list of integers +ref: L = [int(''.join([str(y) for y in x])) for x in L] +Output: [int(x) for x in L] +bleu_metric: 18.058610039563465 +---------- +NL: write the elements of list `lines` concatenated by special character '\n' to file `myfile` +ref: myfile.write('\n'.join(lines)) +Output: myfile.write('\n') +bleu_metric: 51.6288658561904 +---------- +NL: removing an element from a list based on a predicate 'x' or 'n' +ref: [x for x in ['AAT', 'XAC', 'ANT', 'TTA'] if 'X' not in x and 'N' not in x] +Output: [x for x in a▁if x['n'] not in b] +bleu_metric: 15.169494971705259 +---------- +NL: remove duplicate words from a string `text` using regex +ref: text = re.sub('\\b(\\w+)( \\1\\b)+', '\\1', text) +Output: re.sub('\\b\\b', '', text) +bleu_metric: 29.302169335308744 +---------- +NL: count non zero values in each column in pandas data frame +ref: df.astype(bool).sum(axis=1) +Output: df.groupby(level=0).agg(lambda x: x.sum()) +bleu_metric: 16.559113761114784 +---------- +NL: search for string that matches regular expression pattern '(?.*<' in xml string `line` +ref: print(re.search('>.*<', line).group(0)) +Output: ▁print(re.search('>.*<',▁line)) +bleu_metric: 66.21156208217488 +---------- +NL: erase all the contents of a file `filename` +ref: open(filename, 'w').close() +Output: open('filename', 'w').close() +bleu_metric: 70.98108717509044 +---------- +NL: convert a string into datetime using the format '%y-%m-%d %h:%m:%s.%f' +ref: datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S.%f') +Output: ▁datetime.datetime.strptime('%Y-%m-%d %H:%M:%S.%f') +bleu_metric: 88.27916928185874 +---------- +NL: find the index of a list with the first element equal to '332' within the list of lists `thelist` +ref: [index for index, item in enumerate(thelist) if item[0] == '332'] +Output: [i for i, e in enumerate(thelist)▁if e[0] == '332'] +bleu_metric: 56.9717869265984 +---------- +NL: lower a string `text` and remove non-alphanumeric characters aside from space +ref: re.sub('[^\\sa-zA-Z0-9]', '', text).lower().strip() +Output: re.sub('[^0-9a-zA-Z]+', '', text) +bleu_metric: 42.32964405407573 +---------- +NL: remove all non-alphanumeric characters except space from a string `text` and lower it +ref: re.sub('(?!\\s)[\\W_]', '', text).lower().strip() +Output: re.sub('[^0-9a-zA-Z]+', '', text) +bleu_metric: 31.11523617245988 +---------- +NL: subscript text 'h20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. +ref: plt.plot(x, y, label='H\u2082O') +Output: plt.show() +bleu_metric: 6.117046115680378 +---------- +NL: subscript text 'h20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. +ref: plt.plot(x, y, label='$H_2O$') +Output: plt.show() +bleu_metric: 6.117046115680378 +---------- +NL: loop over a list `mylist` if sublists length equals 3 +ref: [x for x in mylist if len(x) == 3] +Output: [len(mylist) for _ in range(3)] +bleu_metric: 15.934326838673723 +---------- +NL: initialize a list `lst` of 100 objects object() +ref: lst = [Object() for _ in range(100)] +Output: lst = [[0] for i in range(100)] +bleu_metric: 54.23782837718303 +---------- +NL: create list `lst` containing 100 instances of object `object` +ref: lst = [Object() for i in range(100)] +Output: [x for x in lst for x in range(100)] +bleu_metric: 44.844165354587766 +---------- +NL: get the content of child tag with`href` attribute whose parent has css `someclass` +ref: self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') +Output: href.get('someclass', '') +bleu_metric: 13.739207814095348 +---------- +NL: joining data from dataframe `df1` with data from dataframe `df2` based on matching values of column 'date_time' in both dataframes +ref: df1.merge(df2, on='Date_Time') +Output: pd.merge(df1, df2,▁left_index=True,▁right_index=True,▁how=' +bleu_metric: 14.071964689317513 +---------- +NL: use `%s` operator to print variable values `str1` inside a string +ref: 'first string is: %s, second one is: %s' % (str1, 'geo.tif') +Output: ▁print(str1, '%s' % ', '.join(str1, str1)) +bleu_metric: 24.754295374108246 +---------- +NL: split a string by a delimiter in python +ref: [x.strip() for x in '2.MATCHES $$TEXT$$ STRING'.split('$$TEXT$$')] +Output: a.split(' ', 1)[0] +bleu_metric: 6.646092160184869 +---------- +NL: check if directory `directory ` exists and create it if necessary +ref: if (not os.path.exists(directory)): + os.makedirs(directory) +Output: ▁print(os.path.exists(directory)) +bleu_metric: 43.84096837774454 +---------- +NL: check if a directory `path` exists and create it if necessary +ref: distutils.dir_util.mkpath(path) +Output: os.path.exists(path) +bleu_metric: 42.728700639623405 +---------- +NL: check if a directory `path` exists and create it if necessary +ref: distutils.dir_util.mkpath(path) +Output: os.path.exists(path) +bleu_metric: 42.728700639623405 +---------- +NL: check if a directory `path` exists and create it if necessary +ref: os.makedirs(path) +Output: os.path.exists(path) +bleu_metric: 45.18010018049224 +---------- +NL: replace a separate word 'h3' by 'h1' in a string 'text' +ref: re.sub('\\bH3\\b', 'H1', text) +Output: re.sub('(?<=[a-z])([A-Z])', '\\1', ' +bleu_metric: 22.898351850650936 +---------- +NL: substitute ascii letters in string 'aas30dsa20' with empty string '' +ref: re.sub('\\D', '', 'aas30dsa20') +Output: """""".join(chr(ord(c)) for c in 'Aas30dSa20')) +bleu_metric: 9.727570367275343 +---------- +NL: get digits only from a string `aas30dsa20` using lambda function +ref: """""".join([x for x in 'aas30dsa20' if x.isdigit()]) +Output: """""".join([c for c in aas30dsa20▁if c.isdigit()]) +bleu_metric: 62.76638518128994 +---------- +NL: access a tag called "name" in beautifulsoup `soup` +ref: print(soup.find('name').string) +Output: ▁print(soup.findAll('name')) +bleu_metric: 56.810968323374965 +---------- +NL: get a dictionary `records` of key-value pairs in pymongo cursor `cursor` +ref: records = dict((record['_id'], record) for record in cursor) +Output: ▁dict([(k, v) for k, v in▁list(cursor.items())]) +bleu_metric: 10.142430673893113 +---------- +NL: create new matrix object by concatenating data from matrix a and matrix b +ref: np.concatenate((A, B)) +Output: ▁numpy.concatenate([(a, b) for a, b in zip(matrix,▁matrix)]) +bleu_metric: 11.002068284361833 +---------- +NL: concat two matrices `a` and `b` in numpy +ref: np.vstack((A, B)) +Output: np.concatenate(a, b) +bleu_metric: 20.34404485471534 +---------- +NL: get the characters count in a file `filepath` +ref: os.stat(filepath).st_size +Output: ▁sum(v for k, v in os.path.values(filepath)▁if v > 0) +bleu_metric: 11.177222547202245 +---------- +NL: count the occurrences of item "a" in list `l` +ref: l.count('a') +Output: ▁sum(1 for i in l▁if i.startswith('a')) +bleu_metric: 24.64832197476761 +---------- +NL: count the occurrences of items in list `l` +ref: Counter(l) +Output: ▁sum(1 for i in l▁if i.startswith('\n') > 1) +bleu_metric: 7.575731225158963 +---------- +NL: count the occurrences of items in list `l` +ref: [[x, l.count(x)] for x in set(l)] +Output: ▁sum(1 for i in l▁if i.startswith('\n') > 1) +bleu_metric: 7.758410052509766 +---------- +NL: count the occurrences of items in list `l` +ref: dict(((x, l.count(x)) for x in set(l))) +Output: ▁sum(1 for i in l▁if i.startswith('\n') > 1) +bleu_metric: 9.009113474307316 +---------- +NL: count the occurrences of item "b" in list `l` +ref: l.count('b') +Output: ▁sum(1 for i, j in zip(l, 'b')▁if j == j) +bleu_metric: 15.450079547146164 +---------- +NL: copy file `srcfile` to directory `dstdir` +ref: shutil.copy(srcfile, dstdir) +Output: ▁shutil.copy(srcfile, dstdir) +bleu_metric: 80.70557274927982 +---------- +NL: find the key associated with the largest value in dictionary `x` whilst key is non-zero value +ref: max(k for k, v in x.items() if v != 0) +Output: max(x,▁key=lambda x: x[0]) +bleu_metric: 11.483394131362408 +---------- +NL: get the largest key whose not associated with value of 0 in dictionary `x` +ref: (k for k, v in x.items() if v != 0) +Output: max(k for k, v in x.items()▁if v▁!= 0) +bleu_metric: 72.00242075875519 +---------- +NL: get the largest key in a dictionary `x` with non-zero value +ref: max(k for k, v in x.items() if v != 0) +Output: max(k for k, v in x.items()▁if v▁!= 0) +bleu_metric: 76.74174160136337 +---------- +NL: put the curser at beginning of the file +ref: file.seek(0) +Output: time.sleep(1) +bleu_metric: 27.776190340117914 +---------- +NL: combine values from column 'b' and column 'a' of dataframe `df` into column 'c' of datafram `df` +ref: df['c'] = np.where(df['a'].isnull, df['b'], df['a']) +Output: df['C'] = df['B'].apply(lambda x: x.astype(int)) +bleu_metric: 15.164081046360186 +---------- +NL: remove key 'ele' from dictionary `d` +ref: del d['ele'] +Output: d =▁dict((k, v) for k, v in d.items()▁if v) +bleu_metric: 5.377336385080629 +---------- +NL: update datetime field in `mymodel` to be the existing `timestamp` plus 100 years +ref: MyModel.objects.update(timestamp=F('timestamp') + timedelta(days=36524.25)) +Output: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime( +bleu_metric: 7.278748444764005 +---------- +NL: merge list `['it']` and list `['was']` and list `['annoying']` into one list +ref: ['it'] + ['was'] + ['annoying'] +Output: ▁list(zip([1'it'], ['is'], ['annoYing'])) +bleu_metric: 19.158743349944555 +---------- +NL: increment a value with leading zeroes in a number `x` +ref: str(int(x) + 1).zfill(len(x)) +Output: ▁numpy.zeros((x + 1) + x) +bleu_metric: 19.950354926643225 +---------- +NL: check if a pandas dataframe `df`'s index is sorted +ref: all(df.index[:-1] <= df.index[1:]) +Output: df.index.sort() +bleu_metric: 7.510537410248026 +---------- +NL: convert tuple `t` to list +ref: list(t) +Output: ▁list(t) +bleu_metric: 68.03749333171201 +---------- +NL: convert list `t` to tuple +ref: tuple(l) +Output: tuple(t) +bleu_metric: 45.180100180492246 +---------- +NL: convert tuple `level1` to list +ref: level1 = map(list, level1) +Output: ▁list(level1.from_tuples()) +bleu_metric: 17.827531042796256 +---------- +NL: send the output of pprint object `dataobject` to file `logfile` +ref: pprint.pprint(dataobject, logFile) +Output: pprint(dataobject, logfile) +bleu_metric: 38.73479811032991 +---------- +NL: get index of rows in column 'boolcol' +ref: df.loc[df['BoolCol']] +Output: df.index +bleu_metric: 2.5225322607845957 +---------- +NL: create a list containing the indexes of rows where the value of column 'boolcol' in dataframe `df` are equal to true +ref: df.iloc[np.flatnonzero(df['BoolCol'])] +Output: df[df.groupby(['boolcol', 'value'])[0].tolist()] +bleu_metric: 17.4932986566737 +---------- +NL: get list of indexes of rows where column 'boolcol' values match true +ref: df[df['BoolCol'] == True].index.tolist() +Output: [i for i, j in enumerate(['boolcol', 'true'])▁if j == j] +bleu_metric: 9.579371674972275 +---------- +NL: get index of rows in dataframe `df` which column 'boolcol' matches value true +ref: df[df['BoolCol']].index.tolist() +Output: df.index = df.loc[df['boolcol'] ==▁value] +bleu_metric: 23.891455818410613 +---------- +NL: change working directory to the directory `owd` +ref: os.chdir(owd) +Output: os.chdir('\\Users\\Desktop\\Desktop\\Desktop\\Desk +bleu_metric: 17.001078098404225 +---------- +NL: insert data from a string `testfield` to sqlite db `c` +ref: c.execute("INSERT INTO test VALUES (?, 'bar')", (testfield,)) +Output: ▁cursor.execute('INSERT▁INTO▁table▁VALUES (%s, %s, %s)', (testfield,) +bleu_metric: 36.151920269854195 +---------- +NL: decode string "\\x89\\n" into a normal string +ref: """\\x89\\n""".decode('string_escape') +Output: """\\x89\\n""".decode('utf-8') +bleu_metric: 79.47545184555568 +---------- +NL: convert a raw string `raw_string` into a normal string +ref: raw_string.decode('string_escape') +Output: ▁raw_string.decode('unicode_escape') +bleu_metric: 53.107253497886994 +---------- +NL: convert a raw string `raw_byte_string` into a normal string +ref: raw_byte_string.decode('unicode_escape') +Output: ▁raw_byte_string.decode('unicode_escape') +bleu_metric: 80.70557274927982 +---------- +NL: split a string `s` with into all strings of repeated characters +ref: [m.group(0) for m in re.finditer('(\\d)\\1*', s)] +Output: re.findall('[^a-zA-Z]*', s) +bleu_metric: 17.036282985955026 +---------- +NL: scatter a plot with x, y position of `np.random.randn(100)` and face color equal to none +ref: plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none') +Output: plt.show() +bleu_metric: 0.8278521686370256 +---------- +NL: do a scatter plot with empty circles +ref: plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none') +Output: plt.show() +bleu_metric: 0.42503347540650505 +---------- +NL: remove a div with a id `main-content` using beautifulsoup +ref: soup.find('div', id='main-content').decompose() +Output: ▁print(soup.findAll('div', class_='maincontent')) +bleu_metric: 34.193725219509716 +---------- +NL: filter rows containing key word `ball` in column `ids` +ref: df[df['ids'].str.contains('ball')] +Output: [(k, v) for k, v in df.ids] +bleu_metric: 11.123500809946101 +---------- +NL: convert index at level 0 into a column in dataframe `df` +ref: df.reset_index(level=0, inplace=True) +Output: df['index'] = df['index'].apply(lambda x: x.tolist()) +bleu_metric: 7.592939129822969 +---------- +NL: add indexes in a data frame `df` to a column `index1` +ref: df['index1'] = df.index +Output: df['index1'] = df['index1'].index +bleu_metric: 55.54570250728591 +---------- +NL: convert pandas index in a dataframe to columns +ref: df.reset_index(level=['tick', 'obs']) +Output: df.set_index(['index', 'index']) +bleu_metric: 32.51279032103401 +---------- +NL: get reverse of list items from list 'b' using extended slicing +ref: [x[::-1] for x in b] +Output: [x[i:i + 2] for i in range(0, len(x), 2)] +bleu_metric: 17.895706401541528 +---------- +NL: join each element in array `a` with element at the same index in array `b` as a tuple +ref: np.array([zip(x, y) for x, y in zip(a, b)]) +Output: A = [(a + b) for a, b in zip(A, B)] +bleu_metric: 18.114844247735682 +---------- +NL: zip two 2-d arrays `a` and `b` +ref: np.array(zip(a.ravel(), b.ravel()), dtype='i4,i4').reshape(a.shape) +Output: np.array(zip(a, b)) +bleu_metric: 10.156383094063225 +---------- +NL: convert list `list_of_ints` into a comma separated string +ref: """,""".join([str(i) for i in list_of_ints]) +Output: """""".join(list_of_ints) +bleu_metric: 31.139742129493257 +---------- +NL: send a post request with raw data `data` and basic authentication with `username` and `password` +ref: requests.post(url, data=DATA, headers=HEADERS_DICT, auth=(username, password)) +Output: r =▁requests.post(url, data=data,▁headers=headers,▁files=passwords) +bleu_metric: 38.57153091307505 +---------- +NL: find last occurrence of character '}' in string "abcd}def}" +ref: 'abcd}def}'.rfind('}') +Output: """abcd}def}""".rfind('}.rsplit('\\d+') +bleu_metric: 42.26469376227503 +---------- +NL: iterate ove list `[1, 2, 3]` using list comprehension +ref: print([item for item in [1, 2, 3]]) +Output: ▁print(list(itertools.chain.from_iterable([1, 2, 3]))) +bleu_metric: 35.3003111274967 +---------- +NL: extract all the values with keys 'x' and 'y' from a list of dictionaries `d` to list of tuples +ref: [(x['x'], x['y']) for x in d] +Output: [d['x'], d['y'], d['d'], d['x'], d[ +bleu_metric: 30.398382447928807 +---------- +NL: get the filename without the extension from file 'hemanth.txt' +ref: print(os.path.splitext(os.path.basename('hemanth.txt'))[0]) +Output: ▁print(os.path.splitext('hemanth.txt')) +bleu_metric: 51.55938317965318 +---------- +NL: create a dictionary by adding each two adjacent elements in tuple `x` as key/value pair to it +ref: dict(x[i:i + 2] for i in range(0, len(x), 2)) +Output: ▁dict(zip(x[::2], x[1::2])) +bleu_metric: 16.45452192914717 +---------- +NL: create a list containing flattened list `[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]` +ref: values = sum([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']], []) +Output: [['a', 'b', 'c'], ['d', 'e', 'f'], [' +bleu_metric: 22.929399628621095 +---------- +NL: select rows in a dataframe `df` column 'closing_price' between two values 99 and 101 +ref: df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)] +Output: df.loc[df['cold_Price'] == 99] +bleu_metric: 9.613222089577718 +---------- +NL: replace all occurences of newlines `\n` with `
` in dataframe `df` +ref: df.replace({'\n': '
'}, regex=True) +Output: df = pd.read_csv('\n').replace('\n', '
') +bleu_metric: 33.87144423286913 +---------- +NL: replace all occurrences of a string `\n` by string `
` in a pandas data frame `df` +ref: df.replace({'\n': '
'}, regex=True) +Output: df = pd.read_csv('\n').replace('\n', '
') +bleu_metric: 33.87144423286913 +---------- +NL: create a list containing each two adjacent letters in string `word` as its elements +ref: [(x + y) for x, y in zip(word, word[1:])] +Output: [word[i:i + 2] for i in range(0, len(word), 2)] +bleu_metric: 15.253223052326929 +---------- +NL: get a list of pairs from a string `word` using lambda function +ref: list(map(lambda x, y: x + y, word[:-1], word[1:])) +Output: ▁list(word.split(',')) +bleu_metric: 7.965309748053327 +---------- +NL: extract a url from a string `mystring` +ref: print(re.findall('(https?://[^\\s]+)', myString)) +Output: re.findall('[^a-zA-Z]*', myString) +bleu_metric: 30.515239072390138 +---------- +NL: extract a url from a string `mystring` +ref: print(re.search('(?Phttps?://[^\\s]+)', myString).group('url')) +Output: re.findall('[^a-zA-Z]*', myString) +bleu_metric: 12.326746139034004 +---------- +NL: remove all special characters, punctuation and spaces from a string `mystring` using regex +ref: re.sub('[^A-Za-z0-9]+', '', mystring) +Output: """""".join(re.sub('\\w+', '', mystring)) +bleu_metric: 40.6731552331291 +---------- +NL: create a datetimeindex containing 13 periods of the second friday of each month starting from date '2016-01-01' +ref: pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13) +Output: ▁datetime.datetime.strptime('2012-01-01', '2012-01-01', '%Y-%m-%d +bleu_metric: 21.54897801937284 +---------- +NL: create multidimensional array `matrix` with 3 rows and 2 columns in python +ref: matrix = [[a, b], [c, d], [e, f]] +Output: ▁matrix = [[[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0]], [ +bleu_metric: 15.448396990132697 +---------- +NL: replace spaces with underscore +ref: mystring.replace(' ', '_') +Output: re.sub('(?<=[a-z])(?=[A-Z])', '\\1' +bleu_metric: 9.500266321476488 +---------- +NL: get an absolute file path of file 'mydir/myfile.txt' +ref: os.path.abspath('mydir/myfile.txt') +Output: os.path.abspath('mydir/myfile.txt') +bleu_metric: 100.0 +---------- +NL: split string `my_string` on white spaces +ref: """ """.join(my_string.split()) +Output: """ """.join(my_string.split()) +bleu_metric: 100.0 +---------- +NL: get filename without extension from file `filename` +ref: os.path.splitext(filename)[0] +Output: os.path.splitext(filename) +bleu_metric: 64.74591278836638 +---------- +NL: get a list containing the sum of each element `i` in list `l` plus the previous elements +ref: [sum(l[:i]) for i, _ in enumerate(l)] +Output: [sum(i) for i, j in zip(l, l)] +bleu_metric: 41.46278264707056 +---------- +NL: split a string `docs/src/scripts/temp` by `/` keeping `/` in the result +ref: """Docs/src/Scripts/temp""".replace('/', '/\x00/').split('\x00') +Output: ▁print(docs/src/scripts/temp.split('/')) +bleu_metric: 17.379851709438395 +---------- +NL: shuffle columns of an numpy array 'r' +ref: np.random.shuffle(np.transpose(r)) +Output: np.random.shuffle(np.random.randn(r)) +bleu_metric: 69.64705665515707 +---------- +NL: copy all values in a column 'b' to a new column 'd' in a pandas data frame 'df' +ref: df['D'] = df['B'] +Output: df['d'] = df['d'].values.tolist() +bleu_metric: 37.392149096896674 +---------- +NL: find a value within nested json 'data' where the key inside another key 'b' is unknown. +ref: list(data['A']['B'].values())[0]['maindata'][0]['Info'] +Output: [item for item in data▁if item['b'] not in item] +bleu_metric: 5.9434130655282855 +---------- +NL: check characters of string `string` are true predication of function `predicate` +ref: all(predicate(x) for x in string) +Output: ▁any(pred(x) for x in▁string.split()) +bleu_metric: 33.92375549949802 +---------- +NL: determine number of files on a drive with python +ref: os.statvfs('/').f_files - os.statvfs('/').f_ffree +Output: ▁sum(i for i in os.listdir('filename')▁if os.path.isfile(filename)) +bleu_metric: 10.12895233093219 +---------- +NL: how to get a single result from a sqlite query in python? +ref: cursor.fetchone()[0] +Output: ▁cursor.execute('INSERT▁INTO▁table▁VALUES (%s, %s, %s)', (var1, var2, +bleu_metric: 5.912329024877923 +---------- +NL: convert string `user_input` into a list of integers `user_list` +ref: user_list = [int(number) for number in user_input.split(',')] +Output: user_list = [int(i) for i in▁user_input.split(',')] +bleu_metric: 66.43548861507492 +---------- +NL: get a list of integers by splitting a string `user` with comma +ref: [int(s) for s in user.split(',')] +Output: [int(x) for x in▁user.split(',')] +bleu_metric: 64.55651607183006 +---------- +NL: sorting a python list by two criteria +ref: sorted(list, key=lambda x: (x[0], -x[1])) +Output: a.sort(key=lambda x: (x[0], x[0]), reverse=True) +bleu_metric: 49.45703137422567 +---------- +NL: sort a list of objects `ut`, based on a function `cmpfun` in descending order +ref: ut.sort(key=cmpfun, reverse=True) +Output: ▁sorted(ut,▁key=lambda x: func(x, -1)) +bleu_metric: 10.813005337959174 +---------- +NL: reverse list `ut` based on the `count` attribute of each object +ref: ut.sort(key=lambda x: x.count, reverse=True) +Output: ▁print(reversed(ut,▁key=lambda x: x.count(), reverse=True)) +bleu_metric: 45.26353562390146 +---------- +NL: sort a list of objects `ut` in reverse order by their `count` property +ref: ut.sort(key=lambda x: x.count, reverse=True) +Output: ▁sorted(ut,▁key=lambda x:▁count.count(x[0], x[0])) +bleu_metric: 20.563241139732103 +---------- +NL: click a href button 'send' with selenium +ref: driver.find_element_by_partial_link_text('Send').click() +Output: ▁driver.find_element_by_css_selector('send') +bleu_metric: 20.083909447408573 +---------- +NL: click a href button having text `send inmail` with selenium +ref: driver.findElement(By.linkText('Send InMail')).click() +Output: ▁driver.find_element_by_css_selector('send inmail.body.send').click() +bleu_metric: 32.326275289498305 +---------- +NL: click a href button with text 'send inmail' with selenium +ref: driver.find_element_by_link_text('Send InMail').click() +Output: ▁driver.find_element_by_css_selector('send inmail').click() +bleu_metric: 48.80217780091719 +---------- +NL: cast an int `i` to a string and concat to string 'me' +ref: 'ME' + str(i) +Output: """""".join(chr(i) for i in range(10)) +bleu_metric: 13.308442527111023 +---------- +NL: sorting data in dataframe pandas +ref: df.sort_values(['System_num', 'Dis']) +Output: df.sort_values(['year','month'],▁ascending=[True, False]) +bleu_metric: 34.52186471009711 +---------- +NL: prepend the line '#test firstline\n' to the contents of file 'infile' and save as the file 'outfile' +ref: open('outfile', 'w').write('#test firstline\n' + open('infile').read()) +Output: open('infile', '#test firstline\n') +bleu_metric: 23.71937737244433 +---------- +NL: sort a list `l` by length of value in tuple +ref: l.sort(key=lambda t: len(t[1]), reverse=True) +Output: ▁sorted(l,▁key=len) +bleu_metric: 8.655647308002333 +---------- +NL: split string `s` by words that ends with 'd' +ref: re.findall('\\b(\\w+)d\\b', s) +Output: re.split('\\s+', s) +bleu_metric: 28.449457729768916 +---------- +NL: return `true` if string `foobarrrr` contains regex `ba[rzd]` +ref: bool(re.search('ba[rzd]', 'foobarrrr')) +Output: ▁print(re.findall('\\b[A-Z]', 'foobarrarrrrrr')) +bleu_metric: 29.452884374019465 +---------- +NL: removing duplicates in list `t` +ref: list(set(t)) +Output: ▁list(set(t)) +bleu_metric: 78.60753021519787 +---------- +NL: removing duplicates in list `source_list` +ref: list(set(source_list)) +Output: ▁print([item for item in▁list(source_list)▁if item.endswith('item')]) +bleu_metric: 11.295714543953919 +---------- +NL: removing duplicates in list `abracadabra` +ref: list(OrderedDict.fromkeys('abracadabra')) +Output: [dict((x / y) for x, y in▁itertools.groupby(lambda x: x.group(0)) +bleu_metric: 8.635800047213174 +---------- +NL: convert array `a` into a list +ref: numpy.array(a).reshape(-1).tolist() +Output: ▁list(array.from_iterable(a)) +bleu_metric: 16.778292801990535 +---------- +NL: convert the first row of numpy matrix `a` to a list +ref: numpy.array(a)[0].tolist() +Output: a = np.array(a) +bleu_metric: 32.2645258949984 +---------- +NL: in `soup`, get the content of the sibling of the `td` tag with text content `address:` +ref: print(soup.find(text='Address:').findNext('td').contents[0]) +Output: soup.find_element_by_xpath("//p[@id='id']/text()/ +bleu_metric: 9.699583930275937 +---------- +NL: convert elements of each tuple in list `l` into a string separated by character `@` +ref: """ """.join([('%d@%d' % t) for t in l]) +Output: """""".join(l[0] for l in zip(l[::2], l[1::2])) +bleu_metric: 32.31269177549822 +---------- +NL: convert each tuple in list `l` to a string with '@' separating the tuples' elements +ref: """ """.join([('%d@%d' % (t[0], t[1])) for t in l]) +Output: [(x, y) for x, y in zip(l, l[::2], l[1::2])] +bleu_metric: 8.274984205902886 +---------- +NL: get the html from the current web page of a selenium driver +ref: driver.execute_script('return document.documentElement.outerHTML;') +Output: ▁driver.find_element_by_css_selector('http://www.google.com/') +bleu_metric: 16.24355752882384 +---------- +NL: get all matches with regex pattern `\\d+[xx]` in list of string `teststr` +ref: [i for i in teststr if re.search('\\d+[xX]', i)] +Output: re.findall('\\d+[xx]', teststr) +bleu_metric: 29.180753193172116 +---------- +NL: select values from column 'a' for which corresponding values in column 'b' will be greater than 50, and in column 'c' - equal 900 in dataframe `df` +ref: df['A'][(df['B'] > 50) & (df['C'] == 900)] +Output: df['b'].str[0].isin(['c', 'a']) +bleu_metric: 12.146852493064019 +---------- +NL: sort dictionary `o` in ascending order based on its keys and items +ref: sorted(o.items()) +Output: ▁sorted(list(o.items()),▁key=lambda x: x[1]) +bleu_metric: 29.452884374019472 +---------- +NL: get sorted list of keys of dict `d` +ref: sorted(d) +Output: ▁sorted(list(d.items()),▁key=lambda x: x[1]) +bleu_metric: 9.043565779655339 +---------- +NL: how to sort dictionaries by keys in python +ref: sorted(d.items()) +Output: ▁sorted(list(d.items()),▁key=lambda x: x[1]) +bleu_metric: 29.452884374019472 +---------- +NL: convert string "1" into integer +ref: int('1') +Output: int('1', 2) +bleu_metric: 58.143073696821936 +---------- +NL: function to convert strings into integers +ref: int() +Output: int(''.join(map(str, s))) +bleu_metric: 12.729922658368398 +---------- +NL: convert items in `t1` to integers +ref: T2 = [map(int, x) for x in T1] +Output: int(''.join(map(str, t1))) +bleu_metric: 12.512236921161914 +---------- +NL: call a shell script `./test.sh` using subprocess +ref: subprocess.call(['./test.sh']) +Output: subprocess.call(['./test.sh']) +bleu_metric: 100.0 +---------- +NL: call a shell script `notepad` using subprocess +ref: subprocess.call(['notepad']) +Output: subprocess.call(['notepad']) +bleu_metric: 100.0 +---------- +NL: combine lists `l1` and `l2` by alternating their elements +ref: [val for pair in zip(l1, l2) for val in pair] +Output: [(x + y) for x, y in zip(l1, l2)] +bleu_metric: 40.59264050928986 +---------- +NL: encode string 'data to be encoded' +ref: encoded = base64.b64encode('data to be encoded') +Output: 'data to be encoded'.encode('data to be encoded') +bleu_metric: 50.495676290868666 +---------- +NL: encode a string `data to be encoded` to `ascii` encoding +ref: encoded = 'data to be encoded'.encode('ascii') +Output: ▁print(ascii.encode('data to be encoded')) +bleu_metric: 51.09375879171261 +---------- +NL: parse tab-delimited csv file 'text.txt' into a list +ref: lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t')) +Output: ['text.txt', 'rb').split(',') +bleu_metric: 35.4953120381003 +---------- +NL: get attribute `my_str` of object `my_object` +ref: getattr(my_object, my_str) +Output: ▁getattr(my_str, my_object) +bleu_metric: 26.26909894424158 +---------- +NL: group a list of dicts `ld` into one dict by key +ref: print(dict(zip(LD[0], zip(*[list(d.values()) for d in LD])))) +Output: mydict = {k: v for k, v in▁list(d.items())} +bleu_metric: 13.839118500778957 +---------- +NL: how do i sum the first value in each tuple in a list of tuples in python? +ref: sum([pair[0] for pair in list_of_pairs]) +Output: ▁sum(x[0] for x in▁rows) +bleu_metric: 30.130404892785684 +---------- +NL: convert unicode string u"{'code1':1,'code2':1}" into dictionary +ref: d = ast.literal_eval("{'code1':1,'code2':1}") +Output: ▁dict(('{'code1':1,'code2':1}'.encode('unicode_escape') +bleu_metric: 61.32878268086118 +---------- +NL: find all words in a string `mystring` that start with the `$` sign +ref: [word for word in mystring.split() if word.startswith('$')] +Output: re.findall('\\w+', mystring) +bleu_metric: 8.395097551291249 +---------- +NL: remove any url within string `text` +ref: text = re.sub('^https?:\\/\\/.*[\\r\\n]*', '', text, flags=re.MULTILINE) +Output: ▁url = re.sub('▁?\\s+', '', text) +bleu_metric: 21.464898124935406 +---------- +NL: replace all elements in array `a` that are not present in array `[1, 3, 4]` with zeros +ref: np.where(np.in1d(A, [1, 3, 4]).reshape(A.shape), A, 0) +Output: np.where(np.isnan(a), [1, 3, 4]) +bleu_metric: 40.9574484442826 +---------- +NL: calculate mean across dimension in a 2d array `a` +ref: np.mean(a, axis=1) +Output: np.mean(a,▁axis=1) +bleu_metric: 76.11606003349893 +---------- +NL: running r script '/pathto/myrscript.r' from python +ref: subprocess.call(['/usr/bin/Rscript', '--vanilla', '/pathto/MyrScript.r']) +Output: os.system('/pathto/myrscript.r') +bleu_metric: 10.462313403993925 +---------- +NL: run r script '/usr/bin/rscript --vanilla /pathto/myrscript.r' +ref: subprocess.call('/usr/bin/Rscript --vanilla /pathto/MyrScript.r', shell=True) +Output: os.system('/usr/bin/rscript --vanilla /pathto/myrscript.r') +bleu_metric: 45.63981491147585 +---------- +NL: add a header to a csv file +ref: writer.writeheader() +Output: ▁writer.writeheader() +bleu_metric: 72.59795291154771 +---------- +NL: replacing nan in the dataframe `df` with row average +ref: df.fillna(df.mean(axis=1), axis=1) +Output: df.fillna(lambda x: x.fillna(x.mean(),▁axis=1)) +bleu_metric: 31.449494592022948 +---------- +NL: convert unix timestamp '1347517370' to formatted string '%y-%m-%d %h:%m:%s' +ref: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) +Output: """%Y-%m-%d %H:%M:%S') +bleu_metric: 57.04022584361268 +---------- +NL: call a base class's class method `do` from derived class `derived` +ref: super(Derived, cls).do(a) +Output: super(Do,▁self).__init__(*args,▁**kwargs) +bleu_metric: 13.26475916741222 +---------- +NL: selecting rows in numpy ndarray 'a', where the value in the first column is 0 and value in the second column is 1 +ref: a[np.where((a[:, (0)] == 0) * (a[:, (1)] == 1))] +Output: A[np.isnan(a[:, (1)])] +bleu_metric: 21.823239874972007 +---------- +NL: separate words delimited by one or more spaces into a list +ref: re.split(' +', 'hello world sample text') +Output: [' '.join(l) for l in re.split('\\s+','', s)] +bleu_metric: 23.977362037775197 +---------- +NL: length of longest element in list `words` +ref: len(max(words, key=len)) +Output: len(words) - len(words) +bleu_metric: 21.258637840736228 +---------- +NL: get the value associated with unicode key 'from_user' of first dictionary in list `result` +ref: result[0]['from_user'] +Output: ▁print([x['from_user'] for x in▁list(result.items())]) +bleu_metric: 19.427446513842177 +---------- +NL: retrieve each line from a file 'file.txt' as a list +ref: [line.split() for line in open('File.txt')] +Output: open('file.txt', 'r') +bleu_metric: 16.725016271836772 +---------- +NL: swap keys with values in a dictionary `a` +ref: res = dict((v, k) for k, v in a.items()) +Output: ▁dict((v, k) for k, v in a.items()) +bleu_metric: 90.21315316859231 +---------- +NL: open a file `path/to/file_name.ext` in write mode +ref: new_file = open('path/to/FILE_NAME.ext', 'w') +Output: os.system('path/to/file_name.ext') +bleu_metric: 40.33687971800765 +---------- +NL: how to count distinct values in a column of a pandas group by object? +ref: df.groupby(['col1', 'col2'])['col3'].nunique().reset_index() +Output: df.groupby(['col1', 'col2']).apply(lambda x: x.groupby(' +bleu_metric: 54.116466213090966 +---------- +NL: check if any key in the dictionary `dict1` starts with the string `emp$$` +ref: any(key.startswith('EMP$$') for key in dict1) +Output: ▁any(k for k, v in▁dict1.items()▁if 'emp$$' in k) +bleu_metric: 13.217947626377288 +---------- +NL: create list of values from dictionary `dict1` that have a key that starts with 'emp$$' +ref: [value for key, value in list(dict1.items()) if key.startswith('EMP$$')] +Output: [d['emp$'] for d in▁dict1.values()] +bleu_metric: 12.254981271073301 +---------- +NL: convert a pandas series `sf` into a pandas dataframe `df` with columns `email` and `list` +ref: pd.DataFrame({'email': sf.index, 'list': sf.values}) +Output: df = pd.DataFrame(sf,▁columns=['email', 'list']) +bleu_metric: 35.69768415530451 +---------- +NL: print elements of list `list` seperated by tabs `\t` +ref: print('\t'.join(map(str, list))) +Output: ▁print('\t'.join(map(str,▁list))) +bleu_metric: 77.39321540474097 +---------- +NL: print unicode string '\xd0\xbf\xd1\x80\xd0\xb8' with utf-8 +ref: print('\xd0\xbf\xd1\x80\xd0\xb8'.encode('raw_unicode_escape')) +Output: ▁print('\xd0\xbf\xd1\x80\xd0\xb8'.encode( +bleu_metric: 75.96292058519337 +---------- +NL: encode a latin character in string `sopet\xc3\xb3n` properly +ref: 'Sopet\xc3\xb3n'.encode('latin-1').decode('utf-8') +Output: """sopet\xc3\xb3n""".encode('latin') +bleu_metric: 38.377836342588495 +---------- +NL: resized image `image` to width, height of `(x, y)` with filter of `antialias` +ref: image = image.resize((x, y), Image.ANTIALIAS) +Output: ▁image.reset_image(image, (x, y)) +bleu_metric: 34.79998616163817 +---------- +NL: regex, find "n"s only in the middle of string `s` +ref: re.findall('n(?<=[^n]n)n+(?=[^n])(?i)', s) +Output: re.findall('\\d+', s) +bleu_metric: 13.004547755725982 +---------- +NL: display the float `1/3*100` as a percentage +ref: print('{0:.0f}%'.format(1.0 / 3 * 100)) +Output: ▁print('{0:.2f}'.format(1/3*100)) +bleu_metric: 65.24084871752925 +---------- +NL: sort a list of dictionary `mylist` by the key `title` +ref: mylist.sort(key=lambda x: x['title']) +Output: mylist.sort(key=lambda x: x['title']) +bleu_metric: 100.0 +---------- +NL: sort a list `l` of dicts by dict value 'title' +ref: l.sort(key=lambda x: x['title']) +Output: ▁sorted(l,▁key=itemgetter('title', 'title')) +bleu_metric: 15.448759309616733 +---------- +NL: sort a list of dictionaries by the value of keys 'title', 'title_url', 'id' in ascending order. +ref: l.sort(key=lambda x: (x['title'], x['title_url'], x['id'])) +Output: ▁sorted(list_of_dicts,▁key=lambda x: x['title']['title'], reverse=Tru +bleu_metric: 34.214468780181804 +---------- +NL: find 10 largest differences between each respective elements of list `l1` and list `l2` +ref: heapq.nlargest(10, range(len(l1)), key=lambda i: abs(l1[i] - l2[i])) +Output: max(l1,▁key=lambda x: (x[1], x[0]), reverse=True) +bleu_metric: 11.878881948379775 +---------- +NL: beautifulsoup find all 'span' elements in html string `soup` with class of 'stargryb sp' +ref: soup.find_all('span', {'class': 'starGryB sp'}) +Output: soup.find_all('span', class_='stargryb.txt') +bleu_metric: 37.90023910380332 +---------- +NL: write records in dataframe `df` to table 'test' in schema 'a_schema' +ref: df.to_sql('test', engine, schema='a_schema') +Output: df.to_csv('test') +bleu_metric: 23.118543024585154 +---------- +NL: extract brackets from string `s` +ref: brackets = re.sub('[^(){}[\\]]', '', s) +Output: re.findall('\\[(\\w+)\\]', s) +bleu_metric: 28.446938709683227 +---------- +NL: remove duplicate elements from list 'l' +ref: list(dict((x[0], x) for x in L).values()) +Output: [x for x in l▁if x not in x] +bleu_metric: 11.000288081592606 +---------- +NL: read a file `file` without newlines +ref: [line.rstrip('\n') for line in file] +Output: open('file', 'w').readline() +bleu_metric: 13.399118318121793 +---------- +NL: get the position of item 1 in `testlist` +ref: [i for (i, x) in enumerate(testlist) if (x == 1)] +Output: ▁print(testlist.position()) +bleu_metric: 6.435022756527756 +---------- +NL: get the position of item 1 in `testlist` +ref: [i for (i, x) in enumerate(testlist) if (x == 1)] +Output: ▁print(testlist.position()) +bleu_metric: 6.435022756527756 +---------- +NL: get the position of item 1 in `testlist` +ref: for i in [i for (i, x) in enumerate(testlist) if (x == 1)]: + pass +Output: ▁print(testlist.position()) +bleu_metric: 4.559019536899008 +---------- +NL: get the position of item 1 in `testlist` +ref: for i in (i for (i, x) in enumerate(testlist) if (x == 1)): + pass +Output: ▁print(testlist.position()) +bleu_metric: 5.045384493046664 +---------- +NL: get the position of item 1 in `testlist` +ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) +for i in gen: + pass +Output: ▁print(testlist.position()) +bleu_metric: 3.8410531655908353 +---------- +NL: get the position of item `element` in list `testlist` +ref: print(testlist.index(element)) +Output: ▁print(testlist.index(element)) +bleu_metric: 82.42367502646054 +---------- +NL: get the position of item `element` in list `testlist` +ref: try: + print(testlist.index(element)) +except ValueError: + pass +Output: ▁print(testlist.index(element)) +bleu_metric: 52.74890952658116 +---------- +NL: find the first element of the tuple with the maximum second element in a list of tuples `lis` +ref: max(lis, key=lambda item: item[1])[0] +Output: max(lis,▁key=lambda x: x[1])[0] +bleu_metric: 60.39435155169263 +---------- +NL: get the item at index 0 from the tuple that has maximum value at index 1 in list `lis` +ref: max(lis, key=itemgetter(1))[0] +Output: max(enumerate(a),▁key=lambda x: x[1])[0] +bleu_metric: 27.274191069381914 +---------- +NL: make a delay of 1 second +ref: time.sleep(1) +Output: time.sleep(1) +bleu_metric: 100.0 +---------- +NL: convert list of tuples `l` to a string +ref: """, """.join('(' + ', '.join(i) + ')' for i in L) +Output: """""".join(l) +bleu_metric: 14.164334116374599 +---------- +NL: django set default value of field `b` equal to '0000000' +ref: b = models.CharField(max_length=7, default='0000000', editable=False) +Output: super(B,▁self).save(*args,▁**kwargs) +bleu_metric: 8.945053454772305 +---------- +NL: sort lis `list5` in ascending order based on the degrees value of its elements +ref: sorted(list5, lambda x: (degree(x), x)) +Output: ▁sorted(list5,▁key=lambda x: int(x[0])) +bleu_metric: 28.571962561926444 +---------- +NL: how do i perform secondary sorting in python? +ref: sorted(list5, key=lambda vertex: (degree(vertex), vertex)) +Output: ▁sorted(list(range(10)),▁key=lambda x: x[1]) +bleu_metric: 15.94000950514835 +---------- +NL: convert a list into a generator object +ref: (n for n in [1, 2, 3, 5]) +Output: ▁list(itertools.product(list(range(10)),▁repeat=4)) +bleu_metric: 8.282282660969601 +---------- +NL: remove elements from list `oldlist` that have an index number mentioned in list `removelist` +ref: newlist = [v for i, v in enumerate(oldlist) if i not in removelist] +Output: [x for x in a▁if x not in b] +bleu_metric: 11.298101090980193 +---------- +NL: open a file `yourfile.txt` in write mode +ref: f = open('yourfile.txt', 'w') +Output: open('yourfile.txt', 'r') +bleu_metric: 67.09548263862051 +---------- +NL: get attribute 'attr' from object `obj` +ref: getattr(obj, 'attr') +Output: ▁getattr(obj, 'attr') +bleu_metric: 80.70557274927982 +---------- +NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to tuple +ref: from functools import reduce +reduce(lambda a, b: a + b, (('aa',), ('bb',), ('cc',))) +Output: tuple(('aa',), ('bb',), ('cc',)) +bleu_metric: 50.621900464488824 +---------- +NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to list in one line +ref: map(lambda a: a[0], (('aa',), ('bb',), ('cc',))) +Output: ▁list(('aa',), ('bb',), ('cc',)) +bleu_metric: 64.0614517827111 +---------- +NL: python pandas: how to replace a characters in a column of a dataframe? +ref: df['range'].replace(',', '-', inplace=True) +Output: df.apply(lambda x: x.replace(',', '').replace(',', '')) +bleu_metric: 30.713730826344694 +---------- +NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` +ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) +Output: zip([('a', 1), ('b', 2), ('c', 3), ('d', 4)] +bleu_metric: 89.10617687473993 +---------- +NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` +ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) +Output: zip([('a', 1), ('b', 2), ('c', 3), ('d', 4)] +bleu_metric: 89.10617687473993 +---------- +NL: unzip list `original` +ref: result = ([a for (a, b) in original], [b for (a, b) in original]) +Output: zip(*Ordered) +bleu_metric: 0.7985269443272172 +---------- +NL: unzip list `original` and return a generator +ref: result = ((a for (a, b) in original), (b for (a, b) in original)) +Output: ▁return (i for i in range(10)▁if i < 0 for i in range(10)) +bleu_metric: 10.587752134048651 +---------- +NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` +ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) +Output: zip([('a', 1), ('b', 2), ('c', 3), ('d', 4), +bleu_metric: 73.36039503547302 +---------- +NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` and fill empty results with none +ref: map(None, *[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) +Output: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4 +bleu_metric: 64.62811194838824 +---------- +NL: encode `decimal('3.9')` to a json string +ref: json.dumps(Decimal('3.9')) +Output: ▁print(json.dumps(decimal('Temperature (\\d+)))) +bleu_metric: 22.656720908801994 +---------- +NL: add key "mynewkey" to dictionary `d` with value "mynewvalue" +ref: d['mynewkey'] = 'mynewvalue' +Output: d = {'mynewkey':'mynewvalue','mynewkey':'mynewvalue'} +bleu_metric: 17.976451428761386 +---------- +NL: add key 'a' to dictionary `data` with value 1 +ref: data.update({'a': 1, }) +Output: data.update({'a': 1, 'b': 1}) +bleu_metric: 63.83240325919925 +---------- +NL: add key 'a' to dictionary `data` with value 1 +ref: data.update(dict(a=1)) +Output: data.update({'a': 1, 'b': 1}) +bleu_metric: 21.951524426618455 +---------- +NL: add key 'a' to dictionary `data` with value 1 +ref: data.update(a=1) +Output: data.update({'a': 1, 'b': 1}) +bleu_metric: 21.951524426618455 +---------- +NL: find maximal value in matrix `matrix` +ref: max([max(i) for i in matrix]) +Output: np.max(matrix,▁axis=0) +bleu_metric: 15.233734832972273 +---------- +NL: round number `answer` to 2 precision after the decimal point +ref: answer = str(round(answer, 2)) +Output: ▁round(answer, 2) +bleu_metric: 49.196255036686594 +---------- +NL: extract ip address from an html string +ref: ip = re.findall('[0-9]+(?:\\.[0-9]+){3}', s) +Output: re.findall('[a-zA-Z0-9-]+', text) +bleu_metric: 20.302505814938357 +---------- +NL: filter dataframe `df` by values in column `a` that appear more than once +ref: df.groupby('A').filter(lambda x: len(x) > 1) +Output: df.filter(['a', 'b']) +bleu_metric: 13.360392338069724 +---------- +NL: append each line in file `myfile` into a list +ref: [x for x in myfile.splitlines() if x != ''] +Output: with open('myfile', 'w').append(line) +bleu_metric: 9.082300379267782 +---------- +NL: get a list of integers `lst` from a file `filename.txt` +ref: lst = map(int, open('filename.txt').readlines()) +Output: [int(x) for x in open('filename.txt', 'lst')] +bleu_metric: 34.46913316729032 +---------- +NL: add color bar with image `mappable` to plot `plt` +ref: plt.colorbar(mappable=mappable, cax=ax3) +Output: plt.show() +bleu_metric: 11.914378950211951 +---------- +NL: count most frequent 100 words in column 'text' of dataframe `df` +ref: Counter(' '.join(df['text']).split()).most_common(100) +Output: df['text'].str.contains(100) +bleu_metric: 27.101674478567933 +---------- +NL: python split a string using regex +ref: re.findall('(.+?):(.+?)\\b ?', text) +Output: re.split('\\s+', s) +bleu_metric: 9.868832370006283 +---------- +NL: generate all 2-element subsets of tuple `(1, 2, 3)` +ref: list(itertools.combinations((1, 2, 3), 2)) +Output: ▁list(itertools.product(list(range(1, 2, 3)),▁repeat=4)) +bleu_metric: 39.47641631338841 +---------- +NL: get a value of datetime.today() in the utc time zone +ref: datetime.now(pytz.utc) +Output: ▁datetime.utcnow().strftime('%Y-%m-%d %H:%M') +bleu_metric: 8.085298080223222 +---------- +NL: get a new list `list2`by removing empty list from a list of lists `list1` +ref: list2 = [x for x in list1 if x != []] +Output: ▁list2 = [x for x in▁list1▁if x is not▁None] +bleu_metric: 39.5373839333436 +---------- +NL: create `list2` to contain the lists from list `list1` excluding the empty lists from `list1` +ref: list2 = [x for x in list1 if x] +Output: ▁list2 = [[] for i in▁list1, 0] +bleu_metric: 18.842393723950337 +---------- +NL: django response with json `data` +ref: return HttpResponse(data, mimetype='application/json') +Output: ▁return HttpResponse(json.dumps(data)) +bleu_metric: 29.361568020583483 +---------- +NL: get all text that is not enclosed within square brackets in string `example_str` +ref: re.findall('(.*?)\\[.*?\\]', example_str) +Output: ▁example_str.split('\\[(\\w+)\\]', '\\[\\]]') +bleu_metric: 23.62618715634458 +---------- +NL: use a regex to get all text in a string `example_str` that is not surrounded by square brackets +ref: re.findall('(.*?)(?:\\[.*?\\]|$)', example_str) +Output: ▁print(re.findall('\\[(\\w+)\\]',▁example_str)) +bleu_metric: 27.808268092146015 +---------- +NL: get whatever is between parentheses as a single match, and any char outside as an individual match in string '(zyx)bc' +ref: re.findall('\\(.+?\\)|\\w', '(zyx)bc') +Output: re.search('(zyx)bc', '(zyx)bc') +bleu_metric: 37.12324840533176 +---------- +NL: match regex '\\((.*?)\\)|(\\w)' with string '(zyx)bc' +ref: re.findall('\\((.*?)\\)|(\\w)', '(zyx)bc') +Output: re.findall('\\((.*?)\\)(\\w)', '(zyx) +bleu_metric: 79.99881739549791 +---------- +NL: match multiple regex patterns with the alternation operator `|` in a string `(zyx)bc` +ref: re.findall('\\(.*?\\)|\\w', '(zyx)bc') +Output: re.findall('(?<=[a-z])(?=[A-Z])', '(zy +bleu_metric: 25.2840740779747 +---------- +NL: formate each string cin list `elements` into pattern '%{0}%' +ref: elements = ['%{0}%'.format(element) for element in elements] +Output: re.sub('[\\W_]+', '', '\\1', '\\1', '\ +bleu_metric: 5.517784889816492 +---------- +NL: open a background process 'background-process' with arguments 'arguments' +ref: subprocess.Popen(['background-process', 'arguments']) +Output: subprocess.call(['background-process', 'arguments']) +bleu_metric: 85.78928092681434 +---------- +NL: get list of values from dictionary 'mydict' w.r.t. list of keys 'mykeys' +ref: [mydict[x] for x in mykeys] +Output: [d['mykey'] for d in mydict] +bleu_metric: 18.69300079996002 +---------- +NL: convert list `[('name', 'joe'), ('age', 22)]` into a dictionary +ref: dict([('Name', 'Joe'), ('Age', 22)]) +Output: ▁dict([('name', 'joe'), ('age', 22)]) +bleu_metric: 58.41177597069678 +---------- +NL: average each two columns of array `data` +ref: data.reshape(-1, j).mean(axis=1).reshape(data.shape[0], -1) +Output: np.mean(np.mean(a.shape[0],▁axis=1)) +bleu_metric: 31.214272516254898 +---------- +NL: double backslash escape all double quotes in string `s` +ref: print(s.encode('unicode-escape').replace('"', '\\"')) +Output: ▁print(s.replace('"', '')) +bleu_metric: 42.801426144397794 +---------- +NL: split a string into a list of words and whitespace +ref: re.split('(\\W+)', s) +Output: [s.split() for s in re.findall('\\S+[^\\S+]+', +bleu_metric: 12.913533075470381 +---------- +NL: plotting stacked barplots on a panda data frame +ref: df.plot(kind='barh', stacked=True) +Output: df.plot(kind='bar',▁stacked=True) +bleu_metric: 66.75075987129311 +---------- +NL: reverse the keys and values in a dictionary `mydictionary` +ref: {i[1]: i[0] for i in list(myDictionary.items())} +Output: ▁dict((v, k) for k, v in mydict.items()) +bleu_metric: 20.467124548606286 +---------- +NL: finding the index of elements containing substring 'how' and 'what' in a list of strings 'mylist'. +ref: [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] +Output: [item for item in mylist▁if 'how' in item] +bleu_metric: 11.11586813181086 +---------- +NL: check if object `obj` is a string +ref: isinstance(obj, str) +Output: isinstance(obj, str) +bleu_metric: 100.0 +---------- +NL: check if object `o` is a string +ref: isinstance(o, str) +Output: isinstance(o, str) +bleu_metric: 100.0 +---------- +NL: check if object `o` is a string +ref: (type(o) is str) +Output: isinstance(o, str) +bleu_metric: 26.78284959130086 +---------- +NL: check if object `o` is a string +ref: isinstance(o, str) +Output: isinstance(o, str) +bleu_metric: 100.0 +---------- +NL: check if `obj_to_test` is a string +ref: isinstance(obj_to_test, str) +Output: isinstance(obj_to_test, str) +bleu_metric: 100.0 +---------- +NL: append list `list1` to `list2` +ref: list2.extend(list1) +Output: ▁list2 = [[] for i in range(1)] +bleu_metric: 9.672649511413093 +---------- +NL: append list `mylog` to `list1` +ref: list1.extend(mylog) +Output: ▁list1.append(mylog) +bleu_metric: 45.18010018049224 +---------- +NL: append list `a` to `c` +ref: c.extend(a) +Output: a.append(c) +bleu_metric: 30.21375397356768 +---------- +NL: append items in list `mylog` to `list1` +ref: for line in mylog: + list1.append(line) +Output: ▁list1.append({'mylog': [1, 2, 3, 4], 'list1': [1, 2, 3, 4]}) +bleu_metric: 11.110969989860408 +---------- +NL: append a tuple of elements from list `a` with indexes '[0][0] [0][2]' to list `b` +ref: b.append((a[0][0], a[0][2])) +Output: [(i, j) for i, j in zip(a, b)] +bleu_metric: 9.575085238714179 +---------- +NL: initialize `secret_key` in flask config with `your_secret_string ` +ref: app.config['SECRET_KEY'] = 'Your_secret_string' +Output: {'SECURITY_KEY': 'your_SECURITY_string'} +bleu_metric: 12.498879161997976 +---------- +NL: unpack a series of tuples in pandas into a dataframe with column names 'out-1' and 'out-2' +ref: pd.DataFrame(out.tolist(), columns=['out-1', 'out-2'], index=out.index) +Output: pd.DataFrame(df.columns,▁columns=['out-1', 'out-2']) +bleu_metric: 55.07111790934335 +---------- +NL: find the index of an element 'msft' in a list `stocks_list` +ref: [x for x in range(len(stocks_list)) if stocks_list[x] == 'MSFT'] +Output: [i for i, v in enumerate(stocks_list.items())▁if v =='msft')▁if v +bleu_metric: 13.33045094117443 +---------- +NL: rotate the xtick labels of matplotlib plot `ax` by `45` degrees to make long labels readable +ref: ax.set_xticklabels(labels, rotation=45) +Output: ax.set_xticklabels(ax.xaxis.get_majorticklabels(45)) +bleu_metric: 30.620490882364876 +---------- +NL: remove symbols from a string `s` +ref: re.sub('[^\\w]', ' ', s) +Output: re.sub('(?<=[a-z])(?=[A-Z])', '', s) +bleu_metric: 35.3639760061202 +---------- +NL: get the current directory of a script +ref: os.path.basename(os.path.dirname(os.path.realpath(__file__))) +Output: os.path.dirname(os.path.realpath(__file__)) +bleu_metric: 63.37967287681905 +---------- +NL: find octal characters matches from a string `str` using regex +ref: print(re.findall("'\\\\[0-7]{1,3}'", str)) +Output: re.findall(pattern,▁string).findall(pattern, re.UNICODE) +bleu_metric: 13.635660702165275 +---------- +NL: split string `input` based on occurrences of regex pattern '[ ](?=[a-z]+\\b)' +ref: re.split('[ ](?=[A-Z]+\\b)', input) +Output: input.split('[ ](?=[a-z]+\\b)') +bleu_metric: 64.58552885189877 +---------- +NL: split string `input` at every space followed by an upper-case letter +ref: re.split('[ ](?=[A-Z])', input) +Output: input.split('.')[1] +bleu_metric: 16.636072310752336 +---------- +NL: send multipart encoded file `files` to url `url` with headers `headers` and metadata `data` +ref: r = requests.post(url, files=files, headers=headers, data=data) +Output: ▁urllib.request.urlopen(url,▁headers=headers,▁headers=data) +bleu_metric: 33.09468095382839 +---------- +NL: write bytes `bytes_` to a file `filename` in python 3 +ref: open('filename', 'wb').write(bytes_) +Output: open('filename', 'rb').write(bytes(bytes_)) +bleu_metric: 60.59827460443867 +---------- +NL: get a list from a list `lst` with values mapped into a dictionary `dct` +ref: [dct[k] for k in lst] +Output: ▁list(itertools.chain.from_iterable(lst)) +bleu_metric: 10.511846841633776 +---------- +NL: find duplicate names in column 'name' of the dataframe `x` +ref: x.set_index('name').index.get_duplicates() +Output: df.groupby('name')['x'].cumsum() +bleu_metric: 34.11488281065382 +---------- +NL: truncate float 1.923328437452 to 3 decimal places +ref: round(1.923328437452, 3) +Output: ▁float('{0:.2f}'.format(env)) +bleu_metric: 9.629943614188138 +---------- +NL: sort list `li` in descending order based on the date value in second element of each list in list `li` +ref: sorted(li, key=lambda x: datetime.strptime(x[1], '%d/%m/%Y'), reverse=True) +Output: ▁sorted(li,▁key=lambda x: (x[1], x[0])) +bleu_metric: 31.993666822129963 +---------- +NL: place the radial ticks in plot `ax` at 135 degrees +ref: ax.set_rlabel_position(135) +Output: ax.set_visible(False) +bleu_metric: 32.17294420803808 +---------- +NL: check if path `my_path` is an absolute path +ref: os.path.isabs(my_path) +Output: ▁print(os.path.abspath(__file__)) +bleu_metric: 29.256127307315065 +---------- +NL: get number of keys in dictionary `yourdict` +ref: len(list(yourdict.keys())) +Output: len(yourdict.keys()) +bleu_metric: 63.70964381207871 +---------- +NL: count the number of keys in dictionary `yourdictfile` +ref: len(set(open(yourdictfile).read().split())) +Output: len(yourdictfile.values()) +bleu_metric: 13.938681520780069 +---------- +NL: pandas dataframe get first row of each group by 'id' +ref: df.groupby('id').first() +Output: df['id'] = df['id'].apply(lambda x: x.tolist()) +bleu_metric: 12.991916506579942 +---------- +NL: split a list in first column into multiple columns keeping other columns as well in pandas data frame +ref: pd.concat([df[0].apply(pd.Series), df[1]], axis=1) +Output: df['A'].str.split(',', 1)['B'].str.split(',') +bleu_metric: 9.64535051675695 +---------- +NL: extract attributes 'src="js/([^"]*\\bjquery\\b[^"]*)"' from string `data` +ref: re.findall('src="js/([^"]*\\bjquery\\b[^"]*)"', data) +Output: re.findall('\\bjquery\\b[^"]*)', data) +bleu_metric: 51.296752626995115 +---------- +NL: sum integers contained in strings in list `['', '3.4', '', '', '1.0']` +ref: sum(int(float(item)) for item in [_f for _f in ['', '3.4', '', '', '1.0'] if _f]) +Output: ▁sum(map(int, ['', '3.4', '', '1.0'])) +bleu_metric: 45.29937105346175 +---------- +NL: call a subprocess with arguments `c:\\program files\\vmware\\vmware server\\vmware-cmd.bat` that may contain spaces +ref: subprocess.Popen(['c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat']) +Output: subprocess.call(['c:\\programfile\\vmware\\vmware-server\\ +bleu_metric: 33.78386474477107 +---------- +NL: reverse a priority queue `q` in python without using classes +ref: q.put((-n, n)) +Output: time.sleep(1) +bleu_metric: 13.597602315271134 +---------- +NL: make a barplot of data in column `group` of dataframe `df` colour-coded according to list `color` +ref: df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r']) +Output: plt.show() +bleu_metric: 0.030120959708599737 +---------- +NL: find all matches of regex pattern '([a-fa-f\\d]{32})' in string `data` +ref: re.findall('([a-fA-F\\d]{32})', data) +Output: re.findall('([a-fa-f\\d]{32})', data) +bleu_metric: 80.59468738950976 +---------- +NL: get the length of list `my_list` +ref: len(my_list) +Output: len(my_list) +bleu_metric: 100.0 +---------- +NL: getting the length of array `l` +ref: len(l) +Output: len(l) +bleu_metric: 100.0 +---------- +NL: getting the length of array `s` +ref: len(s) +Output: len(s) +bleu_metric: 100.0 +---------- +NL: getting the length of `my_tuple` +ref: len(my_tuple) +Output: len(my_tuple) +bleu_metric: 100.0 +---------- +NL: getting the length of `my_string` +ref: len(my_string) +Output: len(my_string) +bleu_metric: 100.0 +---------- +NL: remove escape character from string "\\a" +ref: """\\a""".decode('string_escape') +Output: """\\a""".decode('string_escape') +bleu_metric: 100.0 +---------- +NL: replace each 'a' with 'b' and each 'b' with 'a' in the string 'obama' in a single pass. +ref: """obama""".replace('a', '%temp%').replace('b', 'a').replace('%temp%', 'b') +Output: """""".join(['a', 'b', 'a']) +bleu_metric: 28.029141246256657 +---------- +NL: remove directory tree '/folder_name' +ref: shutil.rmtree('/folder_name') +Output: os.path.dirname('/folder_name') +bleu_metric: 57.608442016038964 +---------- +NL: create a new column `weekday` in pandas data frame `data` based on the values in column `my_dt` +ref: data['weekday'] = data['my_dt'].apply(lambda x: x.weekday()) +Output: data.apply(lambda x: x['weekday'].str.contains(x['my_dt' +bleu_metric: 51.68669917653163 +---------- +NL: reverse sort counter `x` by values +ref: sorted(x, key=x.get, reverse=True) +Output: ▁sorted(x, reverse=True) +bleu_metric: 49.88250584787245 +---------- +NL: reverse sort counter `x` by value +ref: sorted(list(x.items()), key=lambda pair: pair[1], reverse=True) +Output: ▁sorted(x,▁key=itemgetter(1), reverse=True) +bleu_metric: 28.177860103739523 +---------- +NL: append a numpy array 'b' to a numpy array 'a' +ref: np.vstack((a, b)) +Output: ▁numpy.append(a, b) +bleu_metric: 46.92470064105599 +---------- +NL: numpy concatenate two arrays `a` and `b` along the first axis +ref: print(concatenate((a, b), axis=0)) +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 65.60930058975354 +---------- +NL: numpy concatenate two arrays `a` and `b` along the second axis +ref: print(concatenate((a, b), axis=1)) +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 55.60336961016132 +---------- +NL: numpy concatenate two arrays `a` and `b` along the first axis +ref: c = np.r_[(a[None, :], b[None, :])] +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 10.756284573187752 +---------- +NL: numpy concatenate two arrays `a` and `b` along the first axis +ref: np.array((a, b)) +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 41.896825433302276 +---------- +NL: fetch address information for host 'google.com' ion port 80 +ref: print(socket.getaddrinfo('google.com', 80)) +Output: db.request.urlretrieve('google.com') +bleu_metric: 39.00756812161481 +---------- +NL: add a column 'day' with value 'sat' to dataframe `df` +ref: df.xs('sat', level='day', drop_level=False) +Output: df['Date'] = df['Date'].apply(lambda x: x.to_date( +bleu_metric: 7.575731225158963 +---------- +NL: return a 401 unauthorized in django +ref: return HttpResponse('Unauthorized', status=401) +Output: MyModel.objects.filter(author__id=1) +bleu_metric: 14.294845713017917 +---------- +NL: flask set folder 'wherever' as the default template folder +ref: Flask(__name__, template_folder='wherever') +Output: ▁app.run(debug=True) +bleu_metric: 17.03318603763928 +---------- +NL: how do i insert into t1 (select * from t2) in sqlalchemy? +ref: session.execute('INSERT INTO t1 (SELECT * FROM t2)') +Output: ▁session.commit() +bleu_metric: 9.01760872773444 +---------- +NL: sort a list of lists 'c2' such that third row comes first +ref: c2.sort(key=lambda row: row[2]) +Output: c2.sort(key=lambda x: x[0]) +bleu_metric: 55.33409598501608 +---------- +NL: sorting a list of lists in python +ref: c2.sort(key=lambda row: (row[2], row[1], row[0])) +Output: ▁sorted(list_of_lists,▁key=lambda x: x[1]) +bleu_metric: 13.837388732992803 +---------- +NL: sorting a list of lists in python +ref: c2.sort(key=lambda row: (row[2], row[1])) +Output: ▁sorted(list_of_lists,▁key=lambda x: x[1]) +bleu_metric: 23.34445185729352 +---------- +NL: set font `arial` to display non-ascii characters in matplotlib +ref: matplotlib.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'}) +Output: plt.show() +bleu_metric: 0.3669484097406089 +---------- +NL: convert datetime column 'date' of pandas dataframe 'df' to ordinal +ref: df['date'].apply(lambda x: x.toordinal()) +Output: df['date'] = df['date'].dt.to_datetime(df['date'] +bleu_metric: 28.326302347195153 +---------- +NL: get html source of selenium webelement `element` +ref: element.get_attribute('innerHTML') +Output: browser.find_element_by_xpath("//a[@id='a']//a[ +bleu_metric: 9.092617426809149 +---------- +NL: get the integer location of a key `bob` in a pandas data frame +ref: df.index.get_loc('bob') +Output: df.loc[df['bob'].isin()] +bleu_metric: 19.345299022826186 +---------- +NL: open a 'gnome' terminal from python script and run 'sudo apt-get update' command. +ref: os.system('gnome-terminal -e \'bash -c "sudo apt-get update; exec bash"\'') +Output: os.system('sudo apt-get update') +bleu_metric: 21.20024487612511 +---------- +NL: add an item with key 'third_key' and value 1 to an dictionary `my_dict` +ref: my_dict.update({'third_key': 1}) +Output: ▁dict((k, v) for k, v in my_dict.items()▁if v[0] == 1) +bleu_metric: 7.261813302549417 +---------- +NL: declare an array +ref: my_list = [] +Output: intarray = [] +bleu_metric: 75.98356856515926 +---------- +NL: insert item `12` to a list `my_list` +ref: my_list.append(12) +Output: my_list.insert(0, 12) +bleu_metric: 32.46679154750989 +---------- +NL: add an entry 'wuggah' at the beginning of list `mylist` +ref: myList.insert(0, 'wuggah') +Output: ▁list(['wuggah', 'wuggah']) +bleu_metric: 27.22589423069702 +---------- +NL: convert a hex-string representation to actual bytes +ref: """\\xF3\\xBE\\x80\\x80""".replace('\\x', '').decode('hex') +Output: ▁struct.unpack('I', 16))[0] +bleu_metric: 6.258020733367848 +---------- +NL: select the last column of dataframe `df` +ref: df[df.columns[-1]] +Output: df.iloc[:, (-1)] +bleu_metric: 20.78060434846712 +---------- +NL: get the first value from dataframe `df` where column 'letters' is equal to 'c' +ref: df.loc[df['Letters'] == 'C', 'Letters'].values[0] +Output: df['letters'].str.contains('c') +bleu_metric: 12.409088593346615 +---------- +NL: converting two lists `[1, 2, 3]` and `[4, 5, 6]` into a matrix +ref: np.column_stack(([1, 2, 3], [4, 5, 6])) +Output: ▁matrix([[1, 2, 3], [4, 5, 6])) +bleu_metric: 79.78897056262763 +---------- +NL: get the type of `i` +ref: type(i) +Output: type(i) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: get the type of variable `variable_name` +ref: print(type(variable_name)) +Output: ▁globals()['factor'] +bleu_metric: 13.54599427337814 +---------- +NL: get the 5th item of a generator +ref: next(itertools.islice(range(10), 5, 5 + 1)) +Output: ▁list(itertools.islice(it, 5)) +bleu_metric: 31.347458595184502 +---------- +NL: print a string `word` with string format +ref: print('"{}"'.format(word)) +Output: ▁print(' '.join(word)) +bleu_metric: 49.97150634074441 +---------- +NL: join a list of strings `list` using a space ' ' +ref: """ """.join(list) +Output: """ """.join(map(str,▁list)) +bleu_metric: 56.79161104357995 +---------- +NL: create list `y` containing two empty lists +ref: y = [[] for n in range(2)] +Output: y = [[] for i in range(2)] +bleu_metric: 80.03203203844998 +---------- +NL: read a file 'c:/name/mydocuments/numbers' into a list `data` +ref: data = [line.strip() for line in open('C:/name/MyDocuments/numbers', 'r')] +Output: data = [line.read() for▁line in open('C:/name/mydocuments/numbers')] +bleu_metric: 56.78312391912661 +---------- +NL: delete all occurrences of character 'i' in string 'it is icy' +ref: """""".join([char for char in 'it is icy' if char != 'i']) +Output: re.sub('i', 'it is icy') +bleu_metric: 15.99248714366856 +---------- +NL: delete all instances of a character 'i' in a string 'it is icy' +ref: re.sub('i', '', 'it is icy') +Output: re.sub('i', '', 'it is icy') +bleu_metric: 100.0 +---------- +NL: delete all characters "i" in string "it is icy" +ref: """it is icy""".replace('i', '') +Output: """it is icy""".replace('i', '') +bleu_metric: 100.0 +---------- +NL: how to delete all instances of a character in a string in python? +ref: """""".join([char for char in 'it is icy' if char != 'i']) +Output: re.sub('[^a-zA-Z]*', '', text) +bleu_metric: 7.866954533441649 +---------- +NL: drop rows of pandas dataframe `df` having nan in column at index "1" +ref: df.dropna(subset=[1]) +Output: df.dropna(df.columns,▁axis=1) +bleu_metric: 30.130404892785684 +---------- +NL: get elements from list `mylist`, that have a field `n` value 30 +ref: [x for x in myList if x.n == 30] +Output: [i for i, v in enumerate(mylist)▁if v == n] +bleu_metric: 13.229527092256335 +---------- +NL: converting list of strings `intstringlist` to list of integer `nums` +ref: nums = [int(x) for x in intstringlist] +Output: [int(x) for x in intstringlist] +bleu_metric: 83.9234989944319 +---------- +NL: convert list of string numbers into list of integers +ref: map(int, eval(input('Enter the unfriendly numbers: '))) +Output: [int(x) for x in lst] +bleu_metric: 7.290792403968443 +---------- +NL: print "." without newline +ref: sys.stdout.write('.') +Output: ▁sys.stdout.write('.') +bleu_metric: 83.85766789076257 +---------- +NL: round off the float that is the product of `2.52 * 100` and convert it to an int +ref: int(round(2.51 * 100)) +Output: int(round(1.0 *▁100)) +bleu_metric: 46.82568791024402 +---------- +NL: find all files in directory "/mydir" with extension ".txt" +ref: for file in glob.glob('*.txt'): + pass +Output: for▁file in os.listdir('/mydir'):▁if▁file.endswith('.txt'):▁pass +bleu_metric: 20.1857735807738 +---------- +NL: find all files in directory "/mydir" with extension ".txt" +ref: for file in os.listdir('/mydir'): + if file.endswith('.txt'): + pass +Output: for▁file in os.listdir('/mydir'):▁if▁file.endswith('.txt'):▁pass +bleu_metric: 71.95956498085113 +---------- +NL: find all files in directory "/mydir" with extension ".txt" +ref: for (root, dirs, files) in os.walk('/mydir'): + for file in files: + if file.endswith('.txt'): + pass +Output: for▁file in os.listdir('/mydir'):▁if▁file.endswith('.txt'):▁pass +bleu_metric: 47.1684455439837 +---------- +NL: plot dataframe `df` without a legend +ref: df.plot(legend=False) +Output: plt.show() +bleu_metric: 20.034704329441453 +---------- +NL: loop through the ip address range "192.168.x.x" +ref: for i in range(256): + for j in range(256): + ip = ('192.168.%d.%d' % (i, j)) + print(ip) +Output: for i in range('192.168.x.x'):▁pass +bleu_metric: 14.238794877552813 +---------- +NL: loop through the ip address range "192.168.x.x" +ref: for (i, j) in product(list(range(256)), list(range(256))): + pass +Output: for i in range('192.168.x.x'):▁pass +bleu_metric: 7.916713848850043 +---------- +NL: loop through the ip address range "192.168.x.x" +ref: generator = iter_iprange('192.168.1.1', '192.168.255.255', step=1) +Output: for i in range('192.168.x.x'):▁pass +bleu_metric: 18.291291695991816 +---------- +NL: sum the corresponding decimal values for binary values of each boolean element in list `x` +ref: sum(1 << i for i, b in enumerate(x) if b) +Output: ▁sum([int(x) for x in▁list(bool(e))]) +bleu_metric: 14.071964689317513 +---------- +NL: write multiple strings `line1`, `line2` and `line3` in one line in a file `target` +ref: target.write('%r\n%r\n%r\n' % (line1, line2, line3)) +Output: f.write(line1,▁line2 +▁line3.split(',')) +bleu_metric: 16.116608915070366 +---------- +NL: convert list of lists `data` into a flat list +ref: [y for x in data for y in (x if isinstance(x, list) else [x])] +Output: [j for i in data for j in i] +bleu_metric: 9.344244386017191 +---------- +NL: print new line character as `\n` in a string `foo\nbar` +ref: print('foo\nbar'.encode('string_escape')) +Output: """foo\nbar""".replace('\n') +bleu_metric: 32.49189152106538 +---------- +NL: remove last comma character ',' in string `s` +ref: """""".join(s.rsplit(',', 1)) +Output: s = s.replace(',', '') +bleu_metric: 24.4545047029932 +---------- +NL: calculate the mean of each element in array `x` with the element previous to it +ref: (x[1:] + x[:-1]) / 2 +Output: np.mean(x) +bleu_metric: 6.656592803413297 +---------- +NL: get an array of the mean of each two consecutive values in numpy array `x` +ref: x[:-1] + (x[1:] - x[:-1]) / 2 +Output: ▁numpy.array([[1, 2], [2, 4]]) +bleu_metric: 10.64536882179483 +---------- +NL: load data containing `utf-8` from file `new.txt` into numpy array `arr` +ref: arr = numpy.fromiter(codecs.open('new.txt', encoding='utf-8'), dtype=' 1] +Output: set(['a', 'b', 'c']) +bleu_metric: 6.171879938074473 +---------- +NL: Insert directory 'apps' into directory `__file__` +ref: sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps')) +Output: ▁sys.path.apps(os.path.dirname(__file__)) +bleu_metric: 37.386427821086265 +---------- +NL: modify sys.path for python module `subdir` +ref: sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) +Output: ▁sys.path.append(subdir) +bleu_metric: 13.681283243087492 +---------- +NL: Insert a 'None' value into a SQLite3 table. +ref: db.execute("INSERT INTO present VALUES('test2', ?, 10)", (None,)) +Output: db.commit() +bleu_metric: 1.3648974794270936 +---------- +NL: flatten list `list_of_menuitems` +ref: [image for menuitem in list_of_menuitems for image in menuitem] +Output: ▁list_of_enumerate(chain.from_iterable(list_of_menuitems)) +bleu_metric: 11.368272367804307 +---------- +NL: append elements of a set `b` to a list `a` +ref: a.extend(b) +Output: a.append(b) +bleu_metric: 59.46035575013605 +---------- +NL: Append elements of a set to a list in Python +ref: a.extend(list(b)) +Output: a.extend([1, 2, 3]) +bleu_metric: 30.576902884505113 +---------- +NL: write the data of dataframe `df` into text file `np.txt` +ref: np.savetxt('c:\\data\\np.txt', df.values, fmt='%d') +Output: df.to_numeric(np.txt) +bleu_metric: 3.9614496879183108 +---------- +NL: write content of DataFrame `df` into text file 'c:\\data\\pandas.txt' +ref: df.to_csv('c:\\data\\pandas.txt', header=None, index=None, sep=' ', mode='a') +Output: df.to_csv('c:\\data\\pandas.txt') +bleu_metric: 33.15109099076485 +---------- +NL: Split a string `x` by last occurrence of character `-` +ref: print(x.rpartition('-')[0]) +Output: x.rsplit('-', 1) +bleu_metric: 28.24924701979438 +---------- +NL: get the last part of a string before the character '-' +ref: print(x.rsplit('-', 1)[0]) +Output: a[-1:] + a[:-1] +bleu_metric: 9.389419352536649 +---------- +NL: upload file using FTP +ref: ftp.storlines('STOR ' + filename, open(filename, 'r')) +Output: upload_file = open('file.txt', 'wb') +bleu_metric: 14.12701802079611 +---------- +NL: add one to the hidden web element with id 'XYZ' with selenium python script +ref: browser.execute_script("document.getElementById('XYZ').value+='1'") +Output: ▁driver.get('XYZ') +bleu_metric: 13.581073322306715 +---------- +NL: create array containing the maximum value of respective elements of array `[2, 3, 4]` and array `[1, 5, 2]` +ref: np.maximum([2, 3, 4], [1, 5, 2]) +Output: np.argmax([2, 3, 4], [1, 5, 2]) +bleu_metric: 88.67047947918724 +---------- +NL: print a list `l` and move first 3 elements to the end of the list +ref: print(l[3:] + l[:3]) +Output: l = [i for i, l in enumerate(l, 3)▁if l == 3] +bleu_metric: 10.414419091986518 +---------- +NL: loop over files in directory '.' +ref: for fn in os.listdir('.'): + if os.path.isfile(fn): + pass +Output: for item in os.listdir('.') in os.listdir('.'):▁pass +bleu_metric: 48.62438913464417 +---------- +NL: loop over files in directory `source` +ref: for (root, dirs, filenames) in os.walk(source): + for f in filenames: + pass +Output: for (not▁file in os.walk(source)):▁pass +bleu_metric: 34.21915639793568 +---------- +NL: create a random list of integers +ref: [int(1000 * random.random()) for i in range(10000)] +Output: ▁randomList = [random.random() for _ in range(10)] +bleu_metric: 35.22146109090851 +---------- +NL: Using %f with strftime() in Python to get microseconds +ref: datetime.datetime.now().strftime('%H:%M:%S.%f') +Output: ▁print(os.path.getmtime('/tmp/test.txt')) +bleu_metric: 10.400016243706089 +---------- +NL: Google App Engine execute GQL query 'SELECT * FROM Schedule WHERE station = $1' with parameter `foo.key()` +ref: db.GqlQuery('SELECT * FROM Schedule WHERE station = $1', foo.key()) +Output: ▁cursor.execute('SELECT *▁FROM▁your Station = $1,') +bleu_metric: 22.409416775129817 +---------- +NL: filter rows in pandas starting with alphabet 'f' using regular expression. +ref: df.b.str.contains('^f') +Output: df[(df['a'] > 1) & (df['b'] > 1)] +bleu_metric: 6.871624004919695 +---------- +NL: print a 2 dimensional list `tab` as a table with delimiters +ref: print('\n'.join('\t'.join(str(col) for col in row) for row in tab)) +Output: a = [(a[i], b[i]) for i in range(0, len(a), 2)] +bleu_metric: 6.711116671457737 +---------- +NL: pandas: delete rows in dataframe `df` based on multiple columns values +ref: df.set_index(list('BC')).drop(tuples, errors='ignore').reset_index() +Output: df.loc[df.columns == [(df.columns▁!= 0).any(axis=1)] +bleu_metric: 8.722785633659552 +---------- +NL: format the variables `self.goals` and `self.penalties` using string formatting +ref: """({:d} goals, ${:d})""".format(self.goals, self.penalties) +Output: ▁print('{} {}'.format(self.goals,▁self.penalties)) +bleu_metric: 34.66537079680952 +---------- +NL: format string "({} goals, ${})" with variables `goals` and `penalties` +ref: """({} goals, ${})""".format(self.goals, self.penalties) +Output: """({}, ${})""".format(goals, cents) +bleu_metric: 61.17470797490066 +---------- +NL: format string "({0.goals} goals, ${0.penalties})" +ref: """({0.goals} goals, ${0.penalties})""".format(self) +Output: """({0.goals}, ${0.penalties})""".format('${0.go +bleu_metric: 74.24213297217366 +---------- +NL: convert list of lists `L` to list of integers +ref: [int(''.join(str(d) for d in x)) for x in L] +Output: [int(x) for x in L] +bleu_metric: 26.91716452870884 +---------- +NL: combine elements of each list in list `L` into digits of a single integer +ref: [''.join(str(d) for d in x) for x in L] +Output: [int(x) for x in L] +bleu_metric: 31.16211115352542 +---------- +NL: convert a list of lists `L` to list of integers +ref: L = [int(''.join([str(y) for y in x])) for x in L] +Output: [int(x) for x in L] +bleu_metric: 18.058610039563465 +---------- +NL: write the elements of list `lines` concatenated by special character '\n' to file `myfile` +ref: myfile.write('\n'.join(lines)) +Output: myfile.write('\n') +bleu_metric: 51.6288658561904 +---------- +NL: removing an element from a list based on a predicate 'X' or 'N' +ref: [x for x in ['AAT', 'XAC', 'ANT', 'TTA'] if 'X' not in x and 'N' not in x] +Output: [x for x in lst▁if x▁!= 'N') +bleu_metric: 12.603335496309672 +---------- +NL: Remove duplicate words from a string `text` using regex +ref: text = re.sub('\\b(\\w+)( \\1\\b)+', '\\1', text) +Output: re.sub('\\b\\b', '', text) +bleu_metric: 29.302169335308744 +---------- +NL: count non zero values in each column in pandas data frame +ref: df.astype(bool).sum(axis=1) +Output: df.groupby(level=0).agg(lambda x: x.sum()) +bleu_metric: 16.559113761114784 +---------- +NL: search for string that matches regular expression pattern '(?.*<' in xml string `line` +ref: print(re.search('>.*<', line).group(0)) +Output: ▁print(re.search('>.*<',▁line)) +bleu_metric: 66.21156208217488 +---------- +NL: erase all the contents of a file `filename` +ref: open(filename, 'w').close() +Output: open('filename', 'w').close() +bleu_metric: 70.98108717509044 +---------- +NL: convert a string into datetime using the format '%Y-%m-%d %H:%M:%S.%f' +ref: datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S.%f') +Output: ▁datetime.datetime.strptime('%Y-%m-%d %H:%M:%S.%f') +bleu_metric: 88.27916928185874 +---------- +NL: find the index of a list with the first element equal to '332' within the list of lists `thelist` +ref: [index for index, item in enumerate(thelist) if item[0] == '332'] +Output: [i for i, e in enumerate(thelist)▁if e[0] == '332'] +bleu_metric: 56.9717869265984 +---------- +NL: lower a string `text` and remove non-alphanumeric characters aside from space +ref: re.sub('[^\\sa-zA-Z0-9]', '', text).lower().strip() +Output: re.sub('[^0-9a-zA-Z]+', '', text) +bleu_metric: 42.32964405407573 +---------- +NL: remove all non-alphanumeric characters except space from a string `text` and lower it +ref: re.sub('(?!\\s)[\\W_]', '', text).lower().strip() +Output: re.sub('[^0-9a-zA-Z]+', '', text) +bleu_metric: 31.11523617245988 +---------- +NL: subscript text 'H20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. +ref: plt.plot(x, y, label='H\u2082O') +Output: plt.show() +bleu_metric: 6.117046115680378 +---------- +NL: subscript text 'H20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. +ref: plt.plot(x, y, label='$H_2O$') +Output: plt.show() +bleu_metric: 6.117046115680378 +---------- +NL: loop over a list `mylist` if sublists length equals 3 +ref: [x for x in mylist if len(x) == 3] +Output: [len(mylist) for _ in range(3)] +bleu_metric: 15.934326838673723 +---------- +NL: initialize a list `lst` of 100 objects Object() +ref: lst = [Object() for _ in range(100)] +Output: [object(lst) for l in range(100)] +bleu_metric: 51.697315395717055 +---------- +NL: create list `lst` containing 100 instances of object `Object` +ref: lst = [Object() for i in range(100)] +Output: [x for x in lst for x in range(100)] +bleu_metric: 44.844165354587766 +---------- +NL: get the content of child tag with`href` attribute whose parent has css `someclass` +ref: self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') +Output: href.get('someclass', '') +bleu_metric: 13.739207814095348 +---------- +NL: joining data from dataframe `df1` with data from dataframe `df2` based on matching values of column 'Date_Time' in both dataframes +ref: df1.merge(df2, on='Date_Time') +Output: pd.merge(df1, df2,▁how='Date_Time').merge(df2, on='D +bleu_metric: 44.05863767358845 +---------- +NL: use `%s` operator to print variable values `str1` inside a string +ref: 'first string is: %s, second one is: %s' % (str1, 'geo.tif') +Output: ▁print(str1, '%s' % ', '.join(str1, str1)) +bleu_metric: 24.754295374108246 +---------- +NL: Split a string by a delimiter in python +ref: [x.strip() for x in '2.MATCHES $$TEXT$$ STRING'.split('$$TEXT$$')] +Output: re.split('\\s+', s) +bleu_metric: 6.630841478192585 +---------- +NL: check if directory `directory ` exists and create it if necessary +ref: if (not os.path.exists(directory)): + os.makedirs(directory) +Output: ▁print(os.path.exists(directory)) +bleu_metric: 43.84096837774454 +---------- +NL: check if a directory `path` exists and create it if necessary +ref: os.makedirs(path) +Output: os.path.exists(path) +bleu_metric: 45.18010018049224 +---------- +NL: check if a directory `path` exists and create it if necessary +ref: distutils.dir_util.mkpath(path) +Output: os.path.exists(path) +bleu_metric: 42.728700639623405 +---------- +NL: check if a directory `path` exists and create it if necessary +ref: os.makedirs(path) +Output: os.path.exists(path) +bleu_metric: 45.18010018049224 +---------- +NL: Replace a separate word 'H3' by 'H1' in a string 'text' +ref: re.sub('\\bH3\\b', 'H1', text) +Output: """""".join(['H3', 'H1', 'text']) +bleu_metric: 27.186350343875553 +---------- +NL: substitute ASCII letters in string 'aas30dsa20' with empty string '' +ref: re.sub('\\D', '', 'aas30dsa20') +Output: """Aas30dSa20""".replace(' ', '') +bleu_metric: 32.263864160302525 +---------- +NL: get digits only from a string `aas30dsa20` using lambda function +ref: """""".join([x for x in 'aas30dsa20' if x.isdigit()]) +Output: """""".join([c for c in aas30dsa20▁if c.isdigit()]) +bleu_metric: 62.76638518128994 +---------- +NL: access a tag called "name" in beautifulsoup `soup` +ref: print(soup.find('name').string) +Output: ▁print(soup.findAll('name')) +bleu_metric: 56.810968323374965 +---------- +NL: get a dictionary `records` of key-value pairs in PyMongo cursor `cursor` +ref: records = dict((record['_id'], record) for record in cursor) +Output: ▁dict([(k, v) for k, v in▁list(cursor.items())]) +bleu_metric: 10.142430673893113 +---------- +NL: Create new matrix object by concatenating data from matrix A and matrix B +ref: np.concatenate((A, B)) +Output: ▁numpy.concatenate([(A, B)]) +bleu_metric: 41.69392927528885 +---------- +NL: concat two matrices `A` and `B` in numpy +ref: np.vstack((A, B)) +Output: np.concatenate(A, B) +bleu_metric: 49.832529198346116 +---------- +NL: Get the characters count in a file `filepath` +ref: os.stat(filepath).st_size +Output: ▁filepath.count('/path/to/filepath') +bleu_metric: 9.672649511413093 +---------- +NL: count the occurrences of item "a" in list `l` +ref: l.count('a') +Output: ▁sum(1 for i in l▁if i.startswith('a')) +bleu_metric: 24.64832197476761 +---------- +NL: count the occurrences of items in list `l` +ref: Counter(l) +Output: ▁sum(1 for i in l▁if i.startswith('\n') > 1) +bleu_metric: 7.575731225158963 +---------- +NL: count the occurrences of items in list `l` +ref: [[x, l.count(x)] for x in set(l)] +Output: ▁sum(1 for i in l▁if i.startswith('\n') > 1) +bleu_metric: 7.758410052509766 +---------- +NL: count the occurrences of items in list `l` +ref: dict(((x, l.count(x)) for x in set(l))) +Output: ▁sum(1 for i in l▁if i.startswith('\n') > 1) +bleu_metric: 9.009113474307316 +---------- +NL: count the occurrences of item "b" in list `l` +ref: l.count('b') +Output: ▁sum(1 for i, j in zip(l, 'b')▁if j == j) +bleu_metric: 15.450079547146164 +---------- +NL: copy file `srcfile` to directory `dstdir` +ref: shutil.copy(srcfile, dstdir) +Output: ▁shutil.copy(srcfile, dstdir) +bleu_metric: 80.70557274927982 +---------- +NL: find the key associated with the largest value in dictionary `x` whilst key is non-zero value +ref: max(k for k, v in x.items() if v != 0) +Output: max(x,��key=lambda x: x[0]) +bleu_metric: 11.483394131362408 +---------- +NL: get the largest key whose not associated with value of 0 in dictionary `x` +ref: (k for k, v in x.items() if v != 0) +Output: max(k for k, v in x.items()▁if v▁!= 0) +bleu_metric: 72.00242075875519 +---------- +NL: get the largest key in a dictionary `x` with non-zero value +ref: max(k for k, v in x.items() if v != 0) +Output: max(k for k, v in x.items()▁if v▁!= 0) +bleu_metric: 76.74174160136337 +---------- +NL: Put the curser at beginning of the file +ref: file.seek(0) +Output: time.sleep(1) +bleu_metric: 27.776190340117914 +---------- +NL: combine values from column 'b' and column 'a' of dataframe `df` into column 'c' of datafram `df` +ref: df['c'] = np.where(df['a'].isnull, df['b'], df['a']) +Output: df['C'] = df['B'].apply(lambda x: x.astype(int)) +bleu_metric: 15.164081046360186 +---------- +NL: remove key 'ele' from dictionary `d` +ref: del d['ele'] +Output: d =▁dict((k, v) for k, v in d.items()▁if v) +bleu_metric: 5.377336385080629 +---------- +NL: Update datetime field in `MyModel` to be the existing `timestamp` plus 100 years +ref: MyModel.objects.update(timestamp=F('timestamp') + timedelta(days=36524.25)) +Output: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime( +bleu_metric: 7.278748444764005 +---------- +NL: merge list `['it']` and list `['was']` and list `['annoying']` into one list +ref: ['it'] + ['was'] + ['annoying'] +Output: ▁list(zip([1'it'], ['is'], ['annoYing'])) +bleu_metric: 19.158743349944555 +---------- +NL: increment a value with leading zeroes in a number `x` +ref: str(int(x) + 1).zfill(len(x)) +Output: ▁numpy.zeros((x + 1) + x) +bleu_metric: 19.950354926643225 +---------- +NL: check if a pandas dataframe `df`'s index is sorted +ref: all(df.index[:-1] <= df.index[1:]) +Output: df.index.sort() +bleu_metric: 7.510537410248026 +---------- +NL: Convert tuple `t` to list +ref: list(t) +Output: tuple(t) +bleu_metric: 75.98356856515926 +---------- +NL: Convert list `t` to tuple +ref: tuple(l) +Output: tuple(t) +bleu_metric: 45.180100180492246 +---------- +NL: Convert tuple `level1` to list +ref: level1 = map(list, level1) +Output: ▁list(level1.decode('utf-8')) +bleu_metric: 12.03921753741131 +---------- +NL: send the output of pprint object `dataobject` to file `logFile` +ref: pprint.pprint(dataobject, logFile) +Output: pprint(dataobject, logFile) +bleu_metric: 77.8800783071405 +---------- +NL: get index of rows in column 'BoolCol' +ref: df.loc[df['BoolCol']] +Output: df.index +bleu_metric: 2.5225322607845957 +---------- +NL: Create a list containing the indexes of rows where the value of column 'BoolCol' in dataframe `df` are equal to True +ref: df.iloc[np.flatnonzero(df['BoolCol'])] +Output: df[(df['Boolcol'] == 'true']) +bleu_metric: 28.059228101129342 +---------- +NL: get list of indexes of rows where column 'BoolCol' values match True +ref: df[df['BoolCol'] == True].index.tolist() +Output: [i for i, v in enumerate(['Bool', 'Bool'])▁if v == 'true') +bleu_metric: 11.751163762525296 +---------- +NL: get index of rows in dataframe `df` which column 'BoolCol' matches value True +ref: df[df['BoolCol']].index.tolist() +Output: df.index.tolist() +bleu_metric: 28.866139906084342 +---------- +NL: change working directory to the directory `owd` +ref: os.chdir(owd) +Output: os.chdir('\\Users\\Desktop\\Desktop\\Desktop\\Desk +bleu_metric: 17.001078098404225 +---------- +NL: insert data from a string `testfield` to sqlite db `c` +ref: c.execute("INSERT INTO test VALUES (?, 'bar')", (testfield,)) +Output: ▁cursor.execute('INSERT▁INTO▁table▁VALUES (%s, %s, %s)', (testfield,) +bleu_metric: 36.151920269854195 +---------- +NL: decode string "\\x89\\n" into a normal string +ref: """\\x89\\n""".decode('string_escape') +Output: """\\x89\\n""".decode('utf-8') +bleu_metric: 79.47545184555568 +---------- +NL: convert a raw string `raw_string` into a normal string +ref: raw_string.decode('string_escape') +Output: ▁raw_string.decode('unicode_escape') +bleu_metric: 53.107253497886994 +---------- +NL: convert a raw string `raw_byte_string` into a normal string +ref: raw_byte_string.decode('unicode_escape') +Output: ▁raw_byte_string.decode('unicode_escape') +bleu_metric: 80.70557274927982 +---------- +NL: split a string `s` with into all strings of repeated characters +ref: [m.group(0) for m in re.finditer('(\\d)\\1*', s)] +Output: re.findall('[^a-zA-Z]*', s) +bleu_metric: 17.036282985955026 +---------- +NL: scatter a plot with x, y position of `np.random.randn(100)` and face color equal to none +ref: plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none') +Output: plt.show() +bleu_metric: 0.8278521686370256 +---------- +NL: do a scatter plot with empty circles +ref: plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none') +Output: plt.show() +bleu_metric: 0.42503347540650505 +---------- +NL: remove a div with a id `main-content` using beautifulsoup +ref: soup.find('div', id='main-content').decompose() +Output: ▁print(soup.findAll('div', class_='maincontent')) +bleu_metric: 34.193725219509716 +---------- +NL: filter rows containing key word `ball` in column `ids` +ref: df[df['ids'].str.contains('ball')] +Output: [(k, v) for k, v in df.ids] +bleu_metric: 11.123500809946101 +---------- +NL: convert index at level 0 into a column in dataframe `df` +ref: df.reset_index(level=0, inplace=True) +Output: df['index'] = df['index'].apply(lambda x: x.tolist()) +bleu_metric: 7.592939129822969 +---------- +NL: Add indexes in a data frame `df` to a column `index1` +ref: df['index1'] = df.index +Output: df['index1'] = df['index1'].index +bleu_metric: 55.54570250728591 +---------- +NL: convert pandas index in a dataframe to columns +ref: df.reset_index(level=['tick', 'obs']) +Output: df.set_index(['index', 'index']) +bleu_metric: 32.51279032103401 +---------- +NL: Get reverse of list items from list 'b' using extended slicing +ref: [x[::-1] for x in b] +Output: [x[i:i + 2] for i in range(0, len(x), 2)] +bleu_metric: 17.895706401541528 +---------- +NL: join each element in array `a` with element at the same index in array `b` as a tuple +ref: np.array([zip(x, y) for x, y in zip(a, b)]) +Output: A = [(a + b) for a, b in zip(A, B)] +bleu_metric: 18.114844247735682 +---------- +NL: zip two 2-d arrays `a` and `b` +ref: np.array(zip(a.ravel(), b.ravel()), dtype='i4,i4').reshape(a.shape) +Output: np.array(zip(a, b)) +bleu_metric: 10.156383094063225 +---------- +NL: convert list `list_of_ints` into a comma separated string +ref: """,""".join([str(i) for i in list_of_ints]) +Output: """""".join(list_of_ints) +bleu_metric: 31.139742129493257 +---------- +NL: Send a post request with raw data `DATA` and basic authentication with `username` and `password` +ref: requests.post(url, data=DATA, headers=HEADERS_DICT, auth=(username, password)) +Output: r =▁requests.post(url, data=data,▁headers=headers,▁files=passwords) +bleu_metric: 38.57153091307505 +---------- +NL: Find last occurrence of character '}' in string "abcd}def}" +ref: 'abcd}def}'.rfind('}') +Output: """abcd}def}""".rfind('}.rsplit('\\d+') +bleu_metric: 42.26469376227503 +---------- +NL: Iterate ove list `[1, 2, 3]` using list comprehension +ref: print([item for item in [1, 2, 3]]) +Output: [(i, j) for i, j in zip([1, 2, 3])▁if j == j] +bleu_metric: 27.061633707336412 +---------- +NL: extract all the values with keys 'x' and 'y' from a list of dictionaries `d` to list of tuples +ref: [(x['x'], x['y']) for x in d] +Output: [d['x'], d['y'], d['d'], d['x'], d[ +bleu_metric: 30.398382447928807 +---------- +NL: get the filename without the extension from file 'hemanth.txt' +ref: print(os.path.splitext(os.path.basename('hemanth.txt'))[0]) +Output: ▁print(os.path.splitext('hemanth.txt')) +bleu_metric: 51.55938317965318 +---------- +NL: create a dictionary by adding each two adjacent elements in tuple `x` as key/value pair to it +ref: dict(x[i:i + 2] for i in range(0, len(x), 2)) +Output: ▁dict(zip(x[::2], x[1::2])) +bleu_metric: 16.45452192914717 +---------- +NL: create a list containing flattened list `[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]` +ref: values = sum([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']], []) +Output: [['A', 'B', 'C'], ['D', 'E', 'F'], [' +bleu_metric: 53.170929354453335 +---------- +NL: select rows in a dataframe `df` column 'closing_price' between two values 99 and 101 +ref: df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)] +Output: df.loc[df['cold_Price'] == 99] +bleu_metric: 9.613222089577718 +---------- +NL: replace all occurences of newlines `\n` with `
` in dataframe `df` +ref: df.replace({'\n': '
'}, regex=True) +Output: df = pd.read_csv('\n').replace('\n', '
') +bleu_metric: 33.87144423286913 +---------- +NL: replace all occurrences of a string `\n` by string `
` in a pandas data frame `df` +ref: df.replace({'\n': '
'}, regex=True) +Output: df = pd.read_csv('\n').replace('\n', '
') +bleu_metric: 33.87144423286913 +---------- +NL: create a list containing each two adjacent letters in string `word` as its elements +ref: [(x + y) for x, y in zip(word, word[1:])] +Output: [word[i:i + 2] for i in range(0, len(word), 2)] +bleu_metric: 15.253223052326929 +---------- +NL: Get a list of pairs from a string `word` using lambda function +ref: list(map(lambda x, y: x + y, word[:-1], word[1:])) +Output: ▁list(word.split(',')) +bleu_metric: 7.965309748053327 +---------- +NL: extract a url from a string `myString` +ref: print(re.findall('(https?://[^\\s]+)', myString)) +Output: re.findall('\\w+', myString) +bleu_metric: 25.34712979535927 +---------- +NL: extract a url from a string `myString` +ref: print(re.search('(?Phttps?://[^\\s]+)', myString).group('url')) +Output: re.findall('\\w+', myString) +bleu_metric: 8.146914412736555 +---------- +NL: remove all special characters, punctuation and spaces from a string `mystring` using regex +ref: re.sub('[^A-Za-z0-9]+', '', mystring) +Output: """""".join(re.sub('\\w+', '', mystring)) +bleu_metric: 40.6731552331291 +---------- +NL: create a DatetimeIndex containing 13 periods of the second friday of each month starting from date '2016-01-01' +ref: pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13) +Output: ▁datetime.datetime.now().strftime('%Y-%m-%d %H:%M') +bleu_metric: 8.004434939657356 +---------- +NL: Create multidimensional array `matrix` with 3 rows and 2 columns in python +ref: matrix = [[a, b], [c, d], [e, f]] +Output: ▁matrix = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [0, +bleu_metric: 15.448396990132697 +---------- +NL: replace spaces with underscore +ref: mystring.replace(' ', '_') +Output: re.sub('(?<=[a-z])(?=[A-Z])', '\\1' +bleu_metric: 9.500266321476488 +---------- +NL: get an absolute file path of file 'mydir/myfile.txt' +ref: os.path.abspath('mydir/myfile.txt') +Output: os.path.abspath('mydir/myfile.txt') +bleu_metric: 100.0 +---------- +NL: split string `my_string` on white spaces +ref: """ """.join(my_string.split()) +Output: """ """.join(my_string.split()) +bleu_metric: 100.0 +---------- +NL: get filename without extension from file `filename` +ref: os.path.splitext(filename)[0] +Output: os.path.splitext(filename) +bleu_metric: 64.74591278836638 +---------- +NL: get a list containing the sum of each element `i` in list `l` plus the previous elements +ref: [sum(l[:i]) for i, _ in enumerate(l)] +Output: [sum(i) for i, j in zip(l, l)] +bleu_metric: 41.46278264707056 +---------- +NL: split a string `Docs/src/Scripts/temp` by `/` keeping `/` in the result +ref: """Docs/src/Scripts/temp""".replace('/', '/\x00/').split('\x00') +Output: ▁print(docs/src/Scripts/temp.split('/')) +bleu_metric: 23.898608686716166 +---------- +NL: shuffle columns of an numpy array 'r' +ref: np.random.shuffle(np.transpose(r)) +Output: np.random.shuffle(np.random.randn(r)) +bleu_metric: 69.64705665515707 +---------- +NL: copy all values in a column 'B' to a new column 'D' in a pandas data frame 'df' +ref: df['D'] = df['B'] +Output: df['B'] = df['D'].values.tolist() +bleu_metric: 63.53091572554783 +---------- +NL: find a value within nested json 'data' where the key inside another key 'B' is unknown. +ref: list(data['A']['B'].values())[0]['maindata'][0]['Info'] +Output: [item for item in data▁if item['B'] not in item] +bleu_metric: 12.5687883644936 +---------- +NL: check characters of string `string` are true predication of function `predicate` +ref: all(predicate(x) for x in string) +Output: ▁any(pred(x) for x in▁string.split()) +bleu_metric: 33.92375549949802 +---------- +NL: determine number of files on a drive with python +ref: os.statvfs('/').f_files - os.statvfs('/').f_ffree +Output: ▁sum(i for i in os.listdir('filename')▁if os.path.isfile(filename)) +bleu_metric: 10.12895233093219 +---------- +NL: how to get a single result from a SQLite query in python? +ref: cursor.fetchone()[0] +Output: ▁cursor.execute('INSERT▁INTO▁table▁VALUES (%s, %s, %s)', (var1, var2, +bleu_metric: 5.912329024877923 +---------- +NL: convert string `user_input` into a list of integers `user_list` +ref: user_list = [int(number) for number in user_input.split(',')] +Output: user_list = [int(i) for i in▁user_input.split(',')] +bleu_metric: 66.43548861507492 +---------- +NL: Get a list of integers by splitting a string `user` with comma +ref: [int(s) for s in user.split(',')] +Output: [int(x) for x in▁user.split(',')] +bleu_metric: 64.55651607183006 +---------- +NL: Sorting a Python list by two criteria +ref: sorted(list, key=lambda x: (x[0], -x[1])) +Output: a.sort(key=lambda x: (x[0], x[0]), reverse=True) +bleu_metric: 49.45703137422567 +---------- +NL: sort a list of objects `ut`, based on a function `cmpfun` in descending order +ref: ut.sort(key=cmpfun, reverse=True) +Output: ▁sorted(ut,▁key=lambda x: func(x, -1)) +bleu_metric: 10.813005337959174 +---------- +NL: reverse list `ut` based on the `count` attribute of each object +ref: ut.sort(key=lambda x: x.count, reverse=True) +Output: ▁print(reversed(ut,▁key=lambda x: x.count(), reverse=True)) +bleu_metric: 45.26353562390146 +---------- +NL: sort a list of objects `ut` in reverse order by their `count` property +ref: ut.sort(key=lambda x: x.count, reverse=True) +Output: ▁sorted(ut,▁key=lambda x:▁count.count(x[0], x[0])) +bleu_metric: 20.563241139732103 +---------- +NL: click a href button 'Send' with selenium +ref: driver.find_element_by_partial_link_text('Send').click() +Output: ▁driver.find_element_by_css_selector('send') +bleu_metric: 20.083909447408573 +---------- +NL: click a href button having text `Send InMail` with selenium +ref: driver.findElement(By.linkText('Send InMail')).click() +Output: ▁driver.find_element_by_xpath('send inMail').click() +bleu_metric: 33.83154336027537 +---------- +NL: click a href button with text 'Send InMail' with selenium +ref: driver.find_element_by_link_text('Send InMail').click() +Output: ▁driver.find_element_by_css_selector('Send InMail').click() +bleu_metric: 73.51460991014883 +---------- +NL: cast an int `i` to a string and concat to string 'ME' +ref: 'ME' + str(i) +Output: """ME""".encode('utf-8') +bleu_metric: 17.124730448946572 +---------- +NL: Sorting data in DataFrame Pandas +ref: df.sort_values(['System_num', 'Dis']) +Output: df.sort_values(['a', 'b'],▁ascending=[True, False], +bleu_metric: 31.011575752288344 +---------- +NL: prepend the line '#test firstline\n' to the contents of file 'infile' and save as the file 'outfile' +ref: open('outfile', 'w').write('#test firstline\n' + open('infile').read()) +Output: open('infile', '#test firstline\n') +bleu_metric: 23.71937737244433 +---------- +NL: sort a list `l` by length of value in tuple +ref: l.sort(key=lambda t: len(t[1]), reverse=True) +Output: ▁sorted(l,▁key=len) +bleu_metric: 8.655647308002333 +---------- +NL: split string `s` by words that ends with 'd' +ref: re.findall('\\b(\\w+)d\\b', s) +Output: re.split('\\s+', s) +bleu_metric: 28.449457729768916 +---------- +NL: return `True` if string `foobarrrr` contains regex `ba[rzd]` +ref: bool(re.search('ba[rzd]', 'foobarrrr')) +Output: """""".join(re.findall('[^a-zd]*', 'foobarrarrrr +bleu_metric: 15.35259783865636 +---------- +NL: Removing duplicates in list `t` +ref: list(set(t)) +Output: ▁list(set(t)) +bleu_metric: 78.60753021519787 +---------- +NL: Removing duplicates in list `source_list` +ref: list(set(source_list)) +Output: ▁print([item for item in▁list(source_list)▁if item.endswith('item')]) +bleu_metric: 11.295714543953919 +---------- +NL: Removing duplicates in list `abracadabra` +ref: list(OrderedDict.fromkeys('abracadabra')) +Output: [dict((x, x) for x, y in▁itertools.groupby(lambda x: x.group(0)) +bleu_metric: 8.635800047213174 +---------- +NL: Convert array `a` into a list +ref: numpy.array(a).reshape(-1).tolist() +Output: ▁list(array.from_iterable(a)) +bleu_metric: 16.778292801990535 +---------- +NL: Convert the first row of numpy matrix `a` to a list +ref: numpy.array(a)[0].tolist() +Output: a = np.array(a) +bleu_metric: 32.2645258949984 +---------- +NL: In `soup`, get the content of the sibling of the `td` tag with text content `Address:` +ref: print(soup.find(text='Address:').findNext('td').contents[0]) +Output: soup.find_element_by_xpath("//p[@id, 'id']/text()"). +bleu_metric: 12.825436069560483 +---------- +NL: convert elements of each tuple in list `l` into a string separated by character `@` +ref: """ """.join([('%d@%d' % t) for t in l]) +Output: """""".join(l[0] for l in zip(l[::2], l[1::2])) +bleu_metric: 32.31269177549822 +---------- +NL: convert each tuple in list `l` to a string with '@' separating the tuples' elements +ref: """ """.join([('%d@%d' % (t[0], t[1])) for t in l]) +Output: [(x, y) for x, y in zip(l, l[::2], l[1::2])] +bleu_metric: 8.274984205902886 +---------- +NL: get the html from the current web page of a Selenium driver +ref: driver.execute_script('return document.documentElement.outerHTML;') +Output: ▁driver.get('http://stackoverflow.com') +bleu_metric: 18.710158230410627 +---------- +NL: Get all matches with regex pattern `\\d+[xX]` in list of string `teststr` +ref: [i for i in teststr if re.search('\\d+[xX]', i)] +Output: re.findall('\\d+[xX]', teststr) +bleu_metric: 43.99022083366858 +---------- +NL: select values from column 'A' for which corresponding values in column 'B' will be greater than 50, and in column 'C' - equal 900 in dataframe `df` +ref: df['A'][(df['B'] > 50) & (df['C'] == 900)] +Output: df['B'] = df['C'].str.lower() +bleu_metric: 32.3967345710975 +---------- +NL: Sort dictionary `o` in ascending order based on its keys and items +ref: sorted(o.items()) +Output: ▁sorted(list(o.items()),▁key=lambda x: x[1]) +bleu_metric: 29.452884374019472 +---------- +NL: get sorted list of keys of dict `d` +ref: sorted(d) +Output: ▁sorted(list(d.items()),▁key=lambda x: x[1]) +bleu_metric: 9.043565779655339 +---------- +NL: How to sort dictionaries by keys in Python +ref: sorted(d.items()) +Output: ▁sorted(list(d.items()),▁key=lambda x: x[1]) +bleu_metric: 29.452884374019472 +---------- +NL: convert string "1" into integer +ref: int('1') +Output: int('1', 2) +bleu_metric: 58.143073696821936 +---------- +NL: function to convert strings into integers +ref: int() +Output: int(''.join(map(str, s))) +bleu_metric: 12.729922658368398 +---------- +NL: convert items in `T1` to integers +ref: T2 = [map(int, x) for x in T1] +Output: int(''.join(map(str,▁row))) +bleu_metric: 11.75029694362029 +---------- +NL: call a shell script `./test.sh` using subprocess +ref: subprocess.call(['./test.sh']) +Output: subprocess.call(['./test.sh']) +bleu_metric: 100.0 +---------- +NL: call a shell script `notepad` using subprocess +ref: subprocess.call(['notepad']) +Output: subprocess.call(['notepad']) +bleu_metric: 100.0 +---------- +NL: combine lists `l1` and `l2` by alternating their elements +ref: [val for pair in zip(l1, l2) for val in pair] +Output: [(x + y) for x, y in zip(l1, l2)] +bleu_metric: 40.59264050928986 +---------- +NL: encode string 'data to be encoded' +ref: encoded = base64.b64encode('data to be encoded') +Output: 'data to be encoded'.encode('data to be encoded') +bleu_metric: 50.495676290868666 +---------- +NL: encode a string `data to be encoded` to `ascii` encoding +ref: encoded = 'data to be encoded'.encode('ascii') +Output: ▁print(ascii.encode('data to be encoded')) +bleu_metric: 51.09375879171261 +---------- +NL: parse tab-delimited CSV file 'text.txt' into a list +ref: lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t')) +Output: ▁writer.writerows(['text.txt', 'rb').split(',')) +bleu_metric: 43.42718513715163 +---------- +NL: Get attribute `my_str` of object `my_object` +ref: getattr(my_object, my_str) +Output: ▁getattr(my_str, my_object) +bleu_metric: 26.26909894424158 +---------- +NL: group a list of dicts `LD` into one dict by key +ref: print(dict(zip(LD[0], zip(*[list(d.values()) for d in LD])))) +Output: mydict = {k: v for k, v in▁list(LD.items())} +bleu_metric: 9.34970953032271 +---------- +NL: How do I sum the first value in each tuple in a list of tuples in Python? +ref: sum([pair[0] for pair in list_of_pairs]) +Output: ▁sum(x[0] for x in▁rows) +bleu_metric: 30.130404892785684 +---------- +NL: convert unicode string u"{'code1':1,'code2':1}" into dictionary +ref: d = ast.literal_eval("{'code1':1,'code2':1}") +Output: ▁dict(('{'code1':1,'code2':1}'.encode('unicode_escape') +bleu_metric: 61.32878268086118 +---------- +NL: find all words in a string `mystring` that start with the `$` sign +ref: [word for word in mystring.split() if word.startswith('$')] +Output: re.findall('\\w+', mystring) +bleu_metric: 8.395097551291249 +---------- +NL: remove any url within string `text` +ref: text = re.sub('^https?:\\/\\/.*[\\r\\n]*', '', text, flags=re.MULTILINE) +Output: ▁url = re.sub('▁?\\s+', '', text) +bleu_metric: 21.464898124935406 +---------- +NL: replace all elements in array `A` that are not present in array `[1, 3, 4]` with zeros +ref: np.where(np.in1d(A, [1, 3, 4]).reshape(A.shape), A, 0) +Output: np.where(np.isnan(a), [1, 3, 4]) +bleu_metric: 40.9574484442826 +---------- +NL: calculate mean across dimension in a 2d array `a` +ref: np.mean(a, axis=1) +Output: np.mean(a,▁axis=1) +bleu_metric: 76.11606003349893 +---------- +NL: running r script '/pathto/MyrScript.r' from python +ref: subprocess.call(['/usr/bin/Rscript', '--vanilla', '/pathto/MyrScript.r']) +Output: os.system('/pathto/MyrScript.r') +bleu_metric: 20.95597855867423 +---------- +NL: run r script '/usr/bin/Rscript --vanilla /pathto/MyrScript.r' +ref: subprocess.call('/usr/bin/Rscript --vanilla /pathto/MyrScript.r', shell=True) +Output: os.system('/usr/bin/rscript --vanilla /pathto/MyrScript.r') +bleu_metric: 60.11385699767446 +---------- +NL: add a header to a csv file +ref: writer.writeheader() +Output: ▁writer.writeheader() +bleu_metric: 72.59795291154771 +---------- +NL: replacing nan in the dataframe `df` with row average +ref: df.fillna(df.mean(axis=1), axis=1) +Output: df.fillna(lambda x: x.fillna(x.mean(),▁axis=1)) +bleu_metric: 31.449494592022948 +---------- +NL: Convert unix timestamp '1347517370' to formatted string '%Y-%m-%d %H:%M:%S' +ref: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) +Output: time.strftime('.%Y-%m-%d %H:%M:%S') +bleu_metric: 66.6355295714607 +---------- +NL: Call a base class's class method `do` from derived class `Derived` +ref: super(Derived, cls).do(a) +Output: super(Do,▁self).__init__(*args,▁**kwargs) +bleu_metric: 13.26475916741222 +---------- +NL: selecting rows in Numpy ndarray 'a', where the value in the first column is 0 and value in the second column is 1 +ref: a[np.where((a[:, (0)] == 0) * (a[:, (1)] == 1))] +Output: ▁numpy.array([[0, 0, 1, 0], [0, 1, 0], [0, 1, 0], [ +bleu_metric: 5.636775411569542 +---------- +NL: separate words delimited by one or more spaces into a list +ref: re.split(' +', 'hello world sample text') +Output: [' '.join(l) for l in re.split('\\s+','', s)] +bleu_metric: 23.977362037775197 +---------- +NL: length of longest element in list `words` +ref: len(max(words, key=len)) +Output: len(words) - len(words) +bleu_metric: 21.258637840736228 +---------- +NL: get the value associated with unicode key 'from_user' of first dictionary in list `result` +ref: result[0]['from_user'] +Output: ▁print([x['from_user'] for x in▁list(result.items())]) +bleu_metric: 19.427446513842177 +---------- +NL: Retrieve each line from a file 'File.txt' as a list +ref: [line.split() for line in open('File.txt')] +Output: ▁file.write('File.txt') +bleu_metric: 32.500555971011345 +---------- +NL: swap keys with values in a dictionary `a` +ref: res = dict((v, k) for k, v in a.items()) +Output: ▁dict((v, k) for k, v in a.items()) +bleu_metric: 90.21315316859231 +---------- +NL: Open a file `path/to/FILE_NAME.ext` in write mode +ref: new_file = open('path/to/FILE_NAME.ext', 'w') +Output: os.system('path/to/FILE_NAME.ext') +bleu_metric: 55.9978208842951 +---------- +NL: How to count distinct values in a column of a pandas group by object? +ref: df.groupby(['col1', 'col2'])['col3'].nunique().reset_index() +Output: df.groupby(['col1', 'col2']).apply(lambda x: x.groupby(' +bleu_metric: 54.116466213090966 +---------- +NL: Check if any key in the dictionary `dict1` starts with the string `EMP$$` +ref: any(key.startswith('EMP$$') for key in dict1) +Output: ▁any(k for k, v in▁dict1.items()▁if 'EMP$$$' in k) +bleu_metric: 19.91148803162055 +---------- +NL: create list of values from dictionary `dict1` that have a key that starts with 'EMP$$' +ref: [value for key, value in list(dict1.items()) if key.startswith('EMP$$')] +Output: [d['EMP$'] for d in▁dict1.values()] +bleu_metric: 14.693064660889684 +---------- +NL: convert a pandas series `sf` into a pandas dataframe `df` with columns `email` and `list` +ref: pd.DataFrame({'email': sf.index, 'list': sf.values}) +Output: df = pd.DataFrame(sf,▁columns=['email', 'list']) +bleu_metric: 35.69768415530451 +---------- +NL: print elements of list `list` seperated by tabs `\t` +ref: print('\t'.join(map(str, list))) +Output: ▁print('\t'.join(map(str,▁list))) +bleu_metric: 77.39321540474097 +---------- +NL: print unicode string '\xd0\xbf\xd1\x80\xd0\xb8' with utf-8 +ref: print('\xd0\xbf\xd1\x80\xd0\xb8'.encode('raw_unicode_escape')) +Output: ▁print('\xd0\xbf\xd1\x80\xd0\xb8'.encode( +bleu_metric: 75.96292058519337 +---------- +NL: Encode a latin character in string `Sopet\xc3\xb3n` properly +ref: 'Sopet\xc3\xb3n'.encode('latin-1').decode('utf-8') +Output: sopet\xc3\xb3n.encode('latin') +bleu_metric: 21.252609690759098 +---------- +NL: resized image `image` to width, height of `(x, y)` with filter of `ANTIALIAS` +ref: image = image.resize((x, y), Image.ANTIALIAS) +Output: image =▁Image.reset_image(image, (x, y)) +bleu_metric: 37.432615061973365 +---------- +NL: regex, find "n"s only in the middle of string `s` +ref: re.findall('n(?<=[^n]n)n+(?=[^n])(?i)', s) +Output: re.findall('\\d+', s) +bleu_metric: 13.004547755725982 +---------- +NL: display the float `1/3*100` as a percentage +ref: print('{0:.0f}%'.format(1.0 / 3 * 100)) +Output: ▁print('{0:.2f}'.format(1/3*100)) +bleu_metric: 65.24084871752925 +---------- +NL: sort a list of dictionary `mylist` by the key `title` +ref: mylist.sort(key=lambda x: x['title']) +Output: mylist.sort(key=lambda x: x['title']) +bleu_metric: 100.0 +---------- +NL: sort a list `l` of dicts by dict value 'title' +ref: l.sort(key=lambda x: x['title']) +Output: ▁sorted(l,▁key=itemgetter('title', 'title')) +bleu_metric: 15.448759309616733 +---------- +NL: sort a list of dictionaries by the value of keys 'title', 'title_url', 'id' in ascending order. +ref: l.sort(key=lambda x: (x['title'], x['title_url'], x['id'])) +Output: ▁sorted(list_of_dicts,▁key=lambda x: x['title']['title'], reverse=Tru +bleu_metric: 34.214468780181804 +---------- +NL: find 10 largest differences between each respective elements of list `l1` and list `l2` +ref: heapq.nlargest(10, range(len(l1)), key=lambda i: abs(l1[i] - l2[i])) +Output: max(l1,▁key=lambda x: (x[1], x[0]), reverse=True) +bleu_metric: 11.878881948379775 +---------- +NL: BeautifulSoup find all 'span' elements in HTML string `soup` with class of 'starGryB sp' +ref: soup.find_all('span', {'class': 'starGryB sp'}) +Output: soup.findAll('starGryB sp') +bleu_metric: 27.40227985814397 +---------- +NL: write records in dataframe `df` to table 'test' in schema 'a_schema' +ref: df.to_sql('test', engine, schema='a_schema') +Output: df.to_csv('test') +bleu_metric: 23.118543024585154 +---------- +NL: Extract brackets from string `s` +ref: brackets = re.sub('[^(){}[\\]]', '', s) +Output: re.findall('\\[(\\w+)\\]', s) +bleu_metric: 28.446938709683227 +---------- +NL: remove duplicate elements from list 'L' +ref: list(dict((x[0], x) for x in L).values()) +Output: ▁list(set(l)) +bleu_metric: 7.902834459544201 +---------- +NL: read a file `file` without newlines +ref: [line.rstrip('\n') for line in file] +Output: open('file', 'w').readline() +bleu_metric: 13.399118318121793 +---------- +NL: get the position of item 1 in `testlist` +ref: [i for (i, x) in enumerate(testlist) if (x == 1)] +Output: ▁print(testlist.position()) +bleu_metric: 6.435022756527756 +---------- +NL: get the position of item 1 in `testlist` +ref: [i for (i, x) in enumerate(testlist) if (x == 1)] +Output: ▁print(testlist.position()) +bleu_metric: 6.435022756527756 +---------- +NL: get the position of item 1 in `testlist` +ref: for i in [i for (i, x) in enumerate(testlist) if (x == 1)]: + pass +Output: ▁print(testlist.position()) +bleu_metric: 4.559019536899008 +---------- +NL: get the position of item 1 in `testlist` +ref: for i in (i for (i, x) in enumerate(testlist) if (x == 1)): + pass +Output: ▁print(testlist.position()) +bleu_metric: 5.045384493046664 +---------- +NL: get the position of item 1 in `testlist` +ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) +Output: ▁print(testlist.position()) +bleu_metric: 8.036848499107174 +---------- +NL: get the position of item `element` in list `testlist` +ref: print(testlist.index(element)) +Output: ▁print(testlist.index(element)) +bleu_metric: 82.42367502646054 +---------- +NL: get the position of item `element` in list `testlist` +ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) +Output: ▁print(testlist.index(element)) +bleu_metric: 8.792719200156416 +---------- +NL: find the first element of the tuple with the maximum second element in a list of tuples `lis` +ref: max(lis, key=lambda item: item[1])[0] +Output: max(lis,▁key=lambda x: x[1])[0] +bleu_metric: 60.39435155169263 +---------- +NL: get the item at index 0 from the tuple that has maximum value at index 1 in list `lis` +ref: max(lis, key=itemgetter(1))[0] +Output: max(enumerate(a),▁key=lambda x: x[1])[0] +bleu_metric: 27.274191069381914 +---------- +NL: Make a delay of 1 second +ref: time.sleep(1) +Output: time.sleep(1) +bleu_metric: 100.0 +---------- +NL: convert list of tuples `L` to a string +ref: """, """.join('(' + ', '.join(i) + ')' for i in L) +Output: """""".join(l) +bleu_metric: 14.164334116374599 +---------- +NL: Django set default value of field `b` equal to '0000000' +ref: b = models.CharField(max_length=7, default='0000000', editable=False) +Output: super(B,▁self).save(*args,▁**kwargs) +bleu_metric: 8.945053454772305 +---------- +NL: Sort lis `list5` in ascending order based on the degrees value of its elements +ref: sorted(list5, lambda x: (degree(x), x)) +Output: ▁sorted(list5,▁key=lambda x: (int(x)[0], x)) +bleu_metric: 40.802623528764286 +---------- +NL: How do I perform secondary sorting in python? +ref: sorted(list5, key=lambda vertex: (degree(vertex), vertex)) +Output: ▁sorted(list(range(10)),▁key=lambda x: x[1]) +bleu_metric: 15.94000950514835 +---------- +NL: convert a list into a generator object +ref: (n for n in [1, 2, 3, 5]) +Output: ▁list(itertools.product(list(range(10)),▁repeat=4)) +bleu_metric: 8.282282660969601 +---------- +NL: remove elements from list `oldlist` that have an index number mentioned in list `removelist` +ref: newlist = [v for i, v in enumerate(oldlist) if i not in removelist] +Output: [x for x in a▁if x not in b] +bleu_metric: 11.298101090980193 +---------- +NL: Open a file `yourfile.txt` in write mode +ref: f = open('yourfile.txt', 'w') +Output: open('yourfile.txt', 'r') +bleu_metric: 67.09548263862051 +---------- +NL: get attribute 'attr' from object `obj` +ref: getattr(obj, 'attr') +Output: ▁getattr(obj, 'attr') +bleu_metric: 80.70557274927982 +---------- +NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to tuple +ref: from functools import reduce +reduce(lambda a, b: a + b, (('aa',), ('bb',), ('cc',))) +Output: tuple(('aa',), ('bb',), ('cc',)) +bleu_metric: 50.621900464488824 +---------- +NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to list in one line +ref: map(lambda a: a[0], (('aa',), ('bb',), ('cc',))) +Output: ▁list(('aa',), ('bb',), ('cc',)) +bleu_metric: 64.0614517827111 +---------- +NL: Python Pandas: How to replace a characters in a column of a dataframe? +ref: df['range'].replace(',', '-', inplace=True) +Output: df['c'] = df['d'].apply(lambda x: x.replace('-', +bleu_metric: 28.80991254706902 +---------- +NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` +ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) +Output: zip([('a', 1), ('b', 2), ('c', 3), ('d', 4)] +bleu_metric: 89.10617687473993 +---------- +NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` +ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) +Output: zip([('a', 1), ('b', 2), ('c', 3), ('d', 4)] +bleu_metric: 89.10617687473993 +---------- +NL: unzip list `original` +ref: result = ([a for (a, b) in original], [b for (a, b) in original]) +Output: zip(*Ordered) +bleu_metric: 0.7985269443272172 +---------- +NL: unzip list `original` and return a generator +ref: result = ((a for (a, b) in original), (b for (a, b) in original)) +Output: ▁return (i for i in range(10)▁if i < 0 for i in range(10)) +bleu_metric: 10.587752134048651 +---------- +NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` +ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) +Output: zip([('a', 1), ('b', 2), ('c', 3), ('d', 4), +bleu_metric: 73.36039503547302 +---------- +NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` and fill empty results with None +ref: map(None, *[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) +Output: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4 +bleu_metric: 64.62811194838824 +---------- +NL: encode `Decimal('3.9')` to a JSON string +ref: json.dumps(Decimal('3.9')) +Output: ▁print(json.dumps({'Temperature': 'Temperature', 'Temperature': 'Temper +bleu_metric: 17.059573701616802 +---------- +NL: Add key "mynewkey" to dictionary `d` with value "mynewvalue" +ref: d['mynewkey'] = 'mynewvalue' +Output: d = {'mynewkey':'mynewvalue','mynewkey':'mynewvalue'} +bleu_metric: 17.976451428761386 +---------- +NL: Add key 'a' to dictionary `data` with value 1 +ref: data.update({'a': 1, }) +Output: data.update({'a': 1, 'b': 1}) +bleu_metric: 63.83240325919925 +---------- +NL: Add key 'a' to dictionary `data` with value 1 +ref: data.update(dict(a=1)) +Output: data.update({'a': 1, 'b': 1}) +bleu_metric: 21.951524426618455 +---------- +NL: Add key 'a' to dictionary `data` with value 1 +ref: data.update(a=1) +Output: data.update({'a': 1, 'b': 1}) +bleu_metric: 21.951524426618455 +---------- +NL: find maximal value in matrix `matrix` +ref: max([max(i) for i in matrix]) +Output: np.max(matrix,▁axis=0) +bleu_metric: 15.233734832972273 +---------- +NL: Round number `answer` to 2 precision after the decimal point +ref: answer = str(round(answer, 2)) +Output: ▁round(answer, 2) +bleu_metric: 49.196255036686594 +---------- +NL: extract ip address from an html string +ref: ip = re.findall('[0-9]+(?:\\.[0-9]+){3}', s) +Output: re.findall('[a-zA-Z0-9-]+', text) +bleu_metric: 20.302505814938357 +---------- +NL: filter dataframe `df` by values in column `A` that appear more than once +ref: df.groupby('A').filter(lambda x: len(x) > 1) +Output: df['A'] = df['B'].filter(df['A']) +bleu_metric: 15.604242268653643 +---------- +NL: append each line in file `myfile` into a list +ref: [x for x in myfile.splitlines() if x != ''] +Output: with open('myfile', 'w').append(line) +bleu_metric: 9.082300379267782 +---------- +NL: Get a list of integers `lst` from a file `filename.txt` +ref: lst = map(int, open('filename.txt').readlines()) +Output: [int(x) for x in open('filename.txt', 'lst')] +bleu_metric: 34.46913316729032 +---------- +NL: add color bar with image `mappable` to plot `plt` +ref: plt.colorbar(mappable=mappable, cax=ax3) +Output: plt.show() +bleu_metric: 11.914378950211951 +---------- +NL: count most frequent 100 words in column 'text' of dataframe `df` +ref: Counter(' '.join(df['text']).split()).most_common(100) +Output: df['text'].str.contains(100) +bleu_metric: 27.101674478567933 +---------- +NL: Python split a string using regex +ref: re.findall('(.+?):(.+?)\\b ?', text) +Output: re.split('\\s+', s) +bleu_metric: 9.868832370006283 +---------- +NL: generate all 2-element subsets of tuple `(1, 2, 3)` +ref: list(itertools.combinations((1, 2, 3), 2)) +Output: ▁list(itertools.product(list(range(1, 2, 3)),▁repeat=4)) +bleu_metric: 39.47641631338841 +---------- +NL: get a value of datetime.today() in the UTC time zone +ref: datetime.now(pytz.utc) +Output: ▁datetime.utcnow().strftime('%Y-%m-%d %H:%M') +bleu_metric: 8.085298080223222 +---------- +NL: Get a new list `list2`by removing empty list from a list of lists `list1` +ref: list2 = [x for x in list1 if x != []] +Output: ▁list2 = [x for x in▁list1▁if x is not▁None] +bleu_metric: 39.5373839333436 +---------- +NL: Create `list2` to contain the lists from list `list1` excluding the empty lists from `list1` +ref: list2 = [x for x in list1 if x] +Output: ▁list2 = [[] for i in▁list1, 0] +bleu_metric: 18.842393723950337 +---------- +NL: Django response with JSON `data` +ref: return HttpResponse(data, mimetype='application/json') +Output: ▁return HttpResponse(json.dumps(data)) +bleu_metric: 29.361568020583483 +---------- +NL: get all text that is not enclosed within square brackets in string `example_str` +ref: re.findall('(.*?)\\[.*?\\]', example_str) +Output: ▁example_str.split('\\[(\\w+)\\]', '\\[\\]]') +bleu_metric: 23.62618715634458 +---------- +NL: Use a regex to get all text in a string `example_str` that is not surrounded by square brackets +ref: re.findall('(.*?)(?:\\[.*?\\]|$)', example_str) +Output: ▁print(re.findall('\\[(\\w+)\\]',▁example_str)) +bleu_metric: 27.808268092146015 +---------- +NL: get whatever is between parentheses as a single match, and any char outside as an individual match in string '(zyx)bc' +ref: re.findall('\\(.+?\\)|\\w', '(zyx)bc') +Output: re.search('(zyx)bc', '(zyx)bc') +bleu_metric: 37.12324840533176 +---------- +NL: match regex '\\((.*?)\\)|(\\w)' with string '(zyx)bc' +ref: re.findall('\\((.*?)\\)|(\\w)', '(zyx)bc') +Output: re.findall('\\((.*?)\\)(\\w)', '(zyx) +bleu_metric: 79.99881739549791 +---------- +NL: match multiple regex patterns with the alternation operator `|` in a string `(zyx)bc` +ref: re.findall('\\(.*?\\)|\\w', '(zyx)bc') +Output: re.findall('(?<=[a-z])(?=[A-Z])', '(zy +bleu_metric: 25.2840740779747 +---------- +NL: formate each string cin list `elements` into pattern '%{0}%' +ref: elements = ['%{0}%'.format(element) for element in elements] +Output: re.sub('[\\W_]+', '', '\\1', '\\1', '\ +bleu_metric: 5.517784889816492 +---------- +NL: Open a background process 'background-process' with arguments 'arguments' +ref: subprocess.Popen(['background-process', 'arguments']) +Output: subprocess.call(['background-process', 'arguments']) +bleu_metric: 85.78928092681434 +---------- +NL: get list of values from dictionary 'mydict' w.r.t. list of keys 'mykeys' +ref: [mydict[x] for x in mykeys] +Output: [d['mykey'] for d in mydict] +bleu_metric: 18.69300079996002 +---------- +NL: convert list `[('Name', 'Joe'), ('Age', 22)]` into a dictionary +ref: dict([('Name', 'Joe'), ('Age', 22)]) +Output: ▁dict([('Name', 'Joe', 'Age', 22)]) +bleu_metric: 80.40514736345938 +---------- +NL: average each two columns of array `data` +ref: data.reshape(-1, j).mean(axis=1).reshape(data.shape[0], -1) +Output: np.mean(np.mean(a.shape[0],▁axis=1)) +bleu_metric: 31.214272516254898 +---------- +NL: double backslash escape all double quotes in string `s` +ref: print(s.encode('unicode-escape').replace('"', '\\"')) +Output: ▁print(s.replace('"', '')) +bleu_metric: 42.801426144397794 +---------- +NL: split a string into a list of words and whitespace +ref: re.split('(\\W+)', s) +Output: [s.split() for s in re.findall('\\S+[^\\S+]+', +bleu_metric: 12.913533075470381 +---------- +NL: plotting stacked barplots on a panda data frame +ref: df.plot(kind='barh', stacked=True) +Output: df.plot(kind='bar',▁stacked=True) +bleu_metric: 66.75075987129311 +---------- +NL: reverse the keys and values in a dictionary `myDictionary` +ref: {i[1]: i[0] for i in list(myDictionary.items())} +Output: ▁dict((v, k) for k, v in myDict.items()) +bleu_metric: 21.05044094649095 +---------- +NL: finding the index of elements containing substring 'how' and 'what' in a list of strings 'myList'. +ref: [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] +Output: [item for item in myList▁if 'how' in item] +bleu_metric: 12.757789554746044 +---------- +NL: check if object `obj` is a string +ref: isinstance(obj, str) +Output: isinstance(obj, str) +bleu_metric: 100.0 +---------- +NL: check if object `o` is a string +ref: isinstance(o, str) +Output: isinstance(o, str) +bleu_metric: 100.0 +---------- +NL: check if object `o` is a string +ref: (type(o) is str) +Output: isinstance(o, str) +bleu_metric: 26.78284959130086 +---------- +NL: check if object `o` is a string +ref: isinstance(o, str) +Output: isinstance(o, str) +bleu_metric: 100.0 +---------- +NL: check if `obj_to_test` is a string +ref: isinstance(obj_to_test, str) +Output: isinstance(obj_to_test, str) +bleu_metric: 100.0 +---------- +NL: append list `list1` to `list2` +ref: list2.extend(list1) +Output: ▁list2 = [[] for i in range(1)] +bleu_metric: 9.672649511413093 +---------- +NL: append list `mylog` to `list1` +ref: list1.extend(mylog) +Output: ▁list1.append(mylog) +bleu_metric: 45.18010018049224 +---------- +NL: append list `a` to `c` +ref: c.extend(a) +Output: a.append(c) +bleu_metric: 30.21375397356768 +---------- +NL: append items in list `mylog` to `list1` +ref: for line in mylog: + list1.append(line) +Output: ▁list1.append({'mylog': [1, 2, 3, 4], 'list1': [1, 2, 3, 4]}) +bleu_metric: 11.110969989860408 +---------- +NL: append a tuple of elements from list `a` with indexes '[0][0] [0][2]' to list `b` +ref: b.append((a[0][0], a[0][2])) +Output: [(i, j) for i, j in zip(a, b)] +bleu_metric: 9.575085238714179 +---------- +NL: Initialize `SECRET_KEY` in flask config with `Your_secret_string ` +ref: app.config['SECRET_KEY'] = 'Your_secret_string' +Output: {'SECRET_KEY': 'Your_SECURITY_string'} +bleu_metric: 20.33038389239877 +---------- +NL: unpack a series of tuples in pandas into a DataFrame with column names 'out-1' and 'out-2' +ref: pd.DataFrame(out.tolist(), columns=['out-1', 'out-2'], index=out.index) +Output: pd.concat([df1, df2],▁axis=1) +bleu_metric: 5.527540935225947 +---------- +NL: find the index of an element 'MSFT' in a list `stocks_list` +ref: [x for x in range(len(stocks_list)) if stocks_list[x] == 'MSFT'] +Output: from functools import▁reduce▁reduce(lambda s: s.index(s.encode('latin1'))▁if s.index +bleu_metric: 7.022952541634667 +---------- +NL: rotate the xtick labels of matplotlib plot `ax` by `45` degrees to make long labels readable +ref: ax.set_xticklabels(labels, rotation=45) +Output: ax.set_xticklabels(ax.xaxis.get_majorticklabels(45)) +bleu_metric: 30.620490882364876 +---------- +NL: remove symbols from a string `s` +ref: re.sub('[^\\w]', ' ', s) +Output: re.sub('(?<=[a-z])(?=[A-Z])', '', s) +bleu_metric: 35.3639760061202 +---------- +NL: Get the current directory of a script +ref: os.path.basename(os.path.dirname(os.path.realpath(__file__))) +Output: os.path.dirname(os.path.realpath(__file__)) +bleu_metric: 63.37967287681905 +---------- +NL: Find octal characters matches from a string `str` using regex +ref: print(re.findall("'\\\\[0-7]{1,3}'", str)) +Output: re.findall(pattern,▁string).findall(pattern, re.UNICODE) +bleu_metric: 13.635660702165275 +---------- +NL: split string `input` based on occurrences of regex pattern '[ ](?=[A-Z]+\\b)' +ref: re.split('[ ](?=[A-Z]+\\b)', input) +Output: input.split('[ ](?=[A-Z]+\b)') +bleu_metric: 75.20601490106428 +---------- +NL: Split string `input` at every space followed by an upper-case letter +ref: re.split('[ ](?=[A-Z])', input) +Output: input.split('.')[1] +bleu_metric: 16.636072310752336 +---------- +NL: send multipart encoded file `files` to url `url` with headers `headers` and metadata `data` +ref: r = requests.post(url, files=files, headers=headers, data=data) +Output: ▁urllib.request.urlopen(url,▁headers=headers,▁headers=data) +bleu_metric: 33.09468095382839 +---------- +NL: write bytes `bytes_` to a file `filename` in python 3 +ref: open('filename', 'wb').write(bytes_) +Output: open('filename', 'rb').write(bytes(bytes_)) +bleu_metric: 60.59827460443867 +---------- +NL: get a list from a list `lst` with values mapped into a dictionary `dct` +ref: [dct[k] for k in lst] +Output: ▁list(itertools.chain.from_iterable(lst)) +bleu_metric: 10.511846841633776 +---------- +NL: find duplicate names in column 'name' of the dataframe `x` +ref: x.set_index('name').index.get_duplicates() +Output: df.groupby('name')['x'].cumsum() +bleu_metric: 34.11488281065382 +---------- +NL: truncate float 1.923328437452 to 3 decimal places +ref: round(1.923328437452, 3) +Output: ▁float('{0:.2f}'.format(env)) +bleu_metric: 9.629943614188138 +---------- +NL: sort list `li` in descending order based on the date value in second element of each list in list `li` +ref: sorted(li, key=lambda x: datetime.strptime(x[1], '%d/%m/%Y'), reverse=True) +Output: ▁sorted(li,▁key=lambda x: (x[1], x[0])) +bleu_metric: 31.993666822129963 +---------- +NL: place the radial ticks in plot `ax` at 135 degrees +ref: ax.set_rlabel_position(135) +Output: ax.set_visible(False) +bleu_metric: 32.17294420803808 +---------- +NL: check if path `my_path` is an absolute path +ref: os.path.isabs(my_path) +Output: ▁print(os.path.abspath(__file__)) +bleu_metric: 29.256127307315065 +---------- +NL: get number of keys in dictionary `yourdict` +ref: len(list(yourdict.keys())) +Output: len(yourdict.keys()) +bleu_metric: 63.70964381207871 +---------- +NL: count the number of keys in dictionary `yourdictfile` +ref: len(set(open(yourdictfile).read().split())) +Output: len(yourdictfile.values()) +bleu_metric: 13.938681520780069 +---------- +NL: pandas dataframe get first row of each group by 'id' +ref: df.groupby('id').first() +Output: df['id'] = df['id'].apply(lambda x: x.tolist()) +bleu_metric: 12.991916506579942 +---------- +NL: split a list in first column into multiple columns keeping other columns as well in pandas data frame +ref: pd.concat([df[0].apply(pd.Series), df[1]], axis=1) +Output: df['A'].str.split(',', 1)['B'].str.split(',') +bleu_metric: 9.64535051675695 +---------- +NL: extract attributes 'src="js/([^"]*\\bjquery\\b[^"]*)"' from string `data` +ref: re.findall('src="js/([^"]*\\bjquery\\b[^"]*)"', data) +Output: re.findall('\\bjquery\\b[^"]*)', data) +bleu_metric: 51.296752626995115 +---------- +NL: Sum integers contained in strings in list `['', '3.4', '', '', '1.0']` +ref: sum(int(float(item)) for item in [_f for _f in ['', '3.4', '', '', '1.0'] if _f]) +Output: ▁sum(map(int, ['', '3.4', '', '1.0'])) +bleu_metric: 45.29937105346175 +---------- +NL: Call a subprocess with arguments `c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat` that may contain spaces +ref: subprocess.Popen(['c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat']) +Output: subprocess.call(['c:\\Programs\\VMware\\VMware',▁shell=T +bleu_metric: 37.84850418718211 +---------- +NL: reverse a priority queue `q` in python without using classes +ref: q.put((-n, n)) +Output: time.sleep(1) +bleu_metric: 13.597602315271134 +---------- +NL: make a barplot of data in column `group` of dataframe `df` colour-coded according to list `color` +ref: df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r']) +Output: plt.show() +bleu_metric: 0.030120959708599737 +---------- +NL: find all matches of regex pattern '([a-fA-F\\d]{32})' in string `data` +ref: re.findall('([a-fA-F\\d]{32})', data) +Output: re.findall('([a-fA-F\\d]{32})', data) +bleu_metric: 100.0 +---------- +NL: Get the length of list `my_list` +ref: len(my_list) +Output: len(my_list) +bleu_metric: 100.0 +---------- +NL: Getting the length of array `l` +ref: len(l) +Output: len(my_array) +bleu_metric: 45.180100180492246 +---------- +NL: Getting the length of array `s` +ref: len(s) +Output: len(s) +bleu_metric: 100.0 +---------- +NL: Getting the length of `my_tuple` +ref: len(my_tuple) +Output: len(my_tuple) +bleu_metric: 100.0 +---------- +NL: Getting the length of `my_string` +ref: len(my_string) +Output: len(my_string) +bleu_metric: 100.0 +---------- +NL: remove escape character from string "\\a" +ref: """\\a""".decode('string_escape') +Output: """\\a""".decode('string_escape') +bleu_metric: 100.0 +---------- +NL: replace each 'a' with 'b' and each 'b' with 'a' in the string 'obama' in a single pass. +ref: """obama""".replace('a', '%temp%').replace('b', 'a').replace('%temp%', 'b') +Output: """""".join(['a', 'b', 'a']) +bleu_metric: 28.029141246256657 +---------- +NL: remove directory tree '/folder_name' +ref: shutil.rmtree('/folder_name') +Output: os.path.dirname('/folder_name') +bleu_metric: 57.608442016038964 +---------- +NL: create a new column `weekday` in pandas data frame `data` based on the values in column `my_dt` +ref: data['weekday'] = data['my_dt'].apply(lambda x: x.weekday()) +Output: data.apply(lambda x: x['weekday'].str.contains(x['my_dt' +bleu_metric: 51.68669917653163 +---------- +NL: reverse sort Counter `x` by values +ref: sorted(x, key=x.get, reverse=True) +Output: ▁sorted(x,▁key=lambda x: x[::-1]) +bleu_metric: 21.27988282044161 +---------- +NL: reverse sort counter `x` by value +ref: sorted(list(x.items()), key=lambda pair: pair[1], reverse=True) +Output: ▁sorted(x,▁key=itemgetter(1), reverse=True) +bleu_metric: 28.177860103739523 +---------- +NL: append a numpy array 'b' to a numpy array 'a' +ref: np.vstack((a, b)) +Output: ▁numpy.append(a, b) +bleu_metric: 46.92470064105599 +---------- +NL: numpy concatenate two arrays `a` and `b` along the first axis +ref: print(concatenate((a, b), axis=0)) +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 65.60930058975354 +---------- +NL: numpy concatenate two arrays `a` and `b` along the second axis +ref: print(concatenate((a, b), axis=1)) +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 55.60336961016132 +---------- +NL: numpy concatenate two arrays `a` and `b` along the first axis +ref: c = np.r_[(a[None, :], b[None, :])] +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 10.756284573187752 +---------- +NL: numpy concatenate two arrays `a` and `b` along the first axis +ref: np.array((a, b)) +Output: np.concatenate((a, b),▁axis=0) +bleu_metric: 41.896825433302276 +---------- +NL: fetch address information for host 'google.com' ion port 80 +ref: print(socket.getaddrinfo('google.com', 80)) +Output: db.request.urlretrieve('google.com') +bleu_metric: 39.00756812161481 +---------- +NL: add a column 'day' with value 'sat' to dataframe `df` +ref: df.xs('sat', level='day', drop_level=False) +Output: df['Date'] = df['Date'].apply(lambda x: x.to_date( +bleu_metric: 7.575731225158963 +---------- +NL: return a 401 unauthorized in django +ref: return HttpResponse('Unauthorized', status=401) +Output: MyModel.objects.filter(author__id=1) +bleu_metric: 14.294845713017917 +---------- +NL: Flask set folder 'wherever' as the default template folder +ref: Flask(__name__, template_folder='wherever') +Output: ▁app.run(debug=True) +bleu_metric: 17.03318603763928 +---------- +NL: How do I INSERT INTO t1 (SELECT * FROM t2) in SQLAlchemy? +ref: session.execute('INSERT INTO t1 (SELECT * FROM t2)') +Output: ▁session.execute('INSERT▁INTO t1 (SELECT *▁FROM t2)') +bleu_metric: 63.83964846132485 +---------- +NL: sort a list of lists 'c2' such that third row comes first +ref: c2.sort(key=lambda row: row[2]) +Output: c2.sort(key=lambda x: x[0]) +bleu_metric: 55.33409598501608 +---------- +NL: Sorting a list of lists in Python +ref: c2.sort(key=lambda row: (row[2], row[1], row[0])) +Output: ▁sorted(a,▁key=lambda x: x.index(x)) +bleu_metric: 13.258555426461873 +---------- +NL: Sorting a list of lists in Python +ref: c2.sort(key=lambda row: (row[2], row[1])) +Output: ▁sorted(a,▁key=lambda x: x.index(x)) +bleu_metric: 17.024322156140222 +---------- +NL: set font `Arial` to display non-ascii characters in matplotlib +ref: matplotlib.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'}) +Output: plt.show() +bleu_metric: 0.3669484097406089 +---------- +NL: Convert DateTime column 'date' of pandas dataframe 'df' to ordinal +ref: df['date'].apply(lambda x: x.toordinal()) +Output: df['date'] = df['date'].dt.to_datetime(df['date'] +bleu_metric: 28.326302347195153 +---------- +NL: get html source of Selenium WebElement `element` +ref: element.get_attribute('innerHTML') +Output: ▁driver.find_element_by_xpath("//a[@id='a']//a[@id +bleu_metric: 7.9290265068413746 +---------- +NL: Get the integer location of a key `bob` in a pandas data frame +ref: df.index.get_loc('bob') +Output: df.loc[df['bob'].index] +bleu_metric: 22.894156860669913 +---------- +NL: open a 'gnome' terminal from python script and run 'sudo apt-get update' command. +ref: os.system('gnome-terminal -e \'bash -c "sudo apt-get update; exec bash"\'') +Output: os.system('sudo apt-get update') +bleu_metric: 21.20024487612511 +---------- +NL: add an item with key 'third_key' and value 1 to an dictionary `my_dict` +ref: my_dict.update({'third_key': 1}) +Output: ▁dict((k, v) for k, v in my_dict.items()▁if v[0] == 1) +bleu_metric: 7.261813302549417 +---------- +NL: declare an array +ref: my_list = [] +Output: intarray = [] +bleu_metric: 75.98356856515926 +---------- +NL: Insert item `12` to a list `my_list` +ref: my_list.append(12) +Output: my_list.insert(0, 12) +bleu_metric: 32.46679154750989 +---------- +NL: add an entry 'wuggah' at the beginning of list `myList` +ref: myList.insert(0, 'wuggah') +Output: ['wuggah', 'wuggah', 'wuggah'] +bleu_metric: 23.961829057131983 +---------- +NL: convert a hex-string representation to actual bytes +ref: """\\xF3\\xBE\\x80\\x80""".replace('\\x', '').decode('hex') +Output: ▁struct.unpack('I', 16))[0] +bleu_metric: 6.258020733367848 +---------- +NL: select the last column of dataframe `df` +ref: df[df.columns[-1]] +Output: df.iloc[:, (-1)] +bleu_metric: 20.78060434846712 +---------- +NL: get the first value from dataframe `df` where column 'Letters' is equal to 'C' +ref: df.loc[df['Letters'] == 'C', 'Letters'].values[0] +Output: df['Letters'].str.contains('C') +bleu_metric: 27.69697733631702 +---------- +NL: converting two lists `[1, 2, 3]` and `[4, 5, 6]` into a matrix +ref: np.column_stack(([1, 2, 3], [4, 5, 6])) +Output: ▁matrix([[1, 2, 3], [4, 5, 6])) +bleu_metric: 79.78897056262763 +---------- +NL: get the type of `i` +ref: type(i) +Output: type(i) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: determine the type of variable `v` +ref: type(v) +Output: type(v) +bleu_metric: 100.0 +---------- +NL: get the type of variable `variable_name` +ref: print(type(variable_name)) +Output: ▁globals()['factor'] +bleu_metric: 13.54599427337814 +---------- +NL: get the 5th item of a generator +ref: next(itertools.islice(range(10), 5, 5 + 1)) +Output: ▁list(itertools.islice(it, 5)) +bleu_metric: 31.347458595184502 +---------- +NL: Print a string `word` with string format +ref: print('"{}"'.format(word)) +Output: ▁print(word.replace(',', '')) +bleu_metric: 22.31180367999676 +---------- +NL: join a list of strings `list` using a space ' ' +ref: """ """.join(list) +Output: """ """.join(map(str,▁list)) +bleu_metric: 56.79161104357995 +---------- +NL: create list `y` containing two empty lists +ref: y = [[] for n in range(2)] +Output: y = [[] for i in range(2)] +bleu_metric: 80.03203203844998 +---------- +NL: read a file 'C:/name/MyDocuments/numbers' into a list `data` +ref: data = [line.strip() for line in open('C:/name/MyDocuments/numbers', 'r')] +Output: data = [line.read() for▁line in open('C:/name/MyDocuments/numbers')] +bleu_metric: 69.7712347922743 +---------- +NL: delete all occurrences of character 'i' in string 'it is icy' +ref: """""".join([char for char in 'it is icy' if char != 'i']) +Output: re.sub('i', 'it is icy') +bleu_metric: 15.99248714366856 +---------- +NL: delete all instances of a character 'i' in a string 'it is icy' +ref: re.sub('i', '', 'it is icy') +Output: re.sub('i', '', 'it is icy') +bleu_metric: 100.0 +---------- +NL: delete all characters "i" in string "it is icy" +ref: """it is icy""".replace('i', '') +Output: """it is icy""".replace('i', '') +bleu_metric: 100.0 +---------- +NL: How to delete all instances of a character in a string in python? +ref: """""".join([char for char in 'it is icy' if char != 'i']) +Output: re.sub('[^a-zA-Z]*', '', text) +bleu_metric: 7.866954533441649 +---------- +NL: Drop rows of pandas dataframe `df` having NaN in column at index "1" +ref: df.dropna(subset=[1]) +Output: df.dropna(df.loc[:, (None)],▁axis=1) +bleu_metric: 20.82198320914845 +---------- +NL: get elements from list `myList`, that have a field `n` value 30 +ref: [x for x in myList if x.n == 30] +Output: [i for i in myList▁if n == 30] +bleu_metric: 45.06143230242354 +---------- +NL: converting list of strings `intstringlist` to list of integer `nums` +ref: nums = [int(x) for x in intstringlist] +Output: [int(x) for x in intstringlist] +bleu_metric: 83.9234989944319 +---------- +NL: convert list of string numbers into list of integers +ref: map(int, eval(input('Enter the unfriendly numbers: '))) +Output: [int(x) for x in lst] +bleu_metric: 7.290792403968443 +---------- +NL: print "." without newline +ref: sys.stdout.write('.') +Output: ▁sys.stdout.write('.') +bleu_metric: 83.85766789076257 +---------- +NL: round off the float that is the product of `2.52 * 100` and convert it to an int +ref: int(round(2.51 * 100)) +Output: int(round(1.0 *▁100)) +bleu_metric: 46.82568791024402 +---------- +NL: Find all files in directory "/mydir" with extension ".txt" +ref: os.chdir('/mydir') +for file in glob.glob('*.txt'): + pass +Output: for▁file in os.listdir('/mydir'):▁if▁file.endswith('.txt'):▁pass +bleu_metric: 37.56995795800578 +---------- +NL: Find all files in directory "/mydir" with extension ".txt" +ref: for file in os.listdir('/mydir'): + if file.endswith('.txt'): + pass +Output: for▁file in os.listdir('/mydir'):▁if▁file.endswith('.txt'):▁pass +bleu_metric: 71.95956498085113 +---------- +NL: Find all files in directory "/mydir" with extension ".txt" +ref: for (root, dirs, files) in os.walk('/mydir'): + for file in files: + if file.endswith('.txt'): + pass +Output: for▁file in os.listdir('/mydir'):▁if▁file.endswith('.txt'):▁pass +bleu_metric: 47.1684455439837 +---------- +NL: plot dataframe `df` without a legend +ref: df.plot(legend=False) +Output: plt.show() +bleu_metric: 20.034704329441453 +---------- +NL: loop through the IP address range "192.168.x.x" +ref: for i in range(256): + for j in range(256): + ip = ('192.168.%d.%d' % (i, j)) + print(ip) +Output: for i in range('192.168.x.x'):▁pass +bleu_metric: 14.238794877552813 +---------- +NL: loop through the IP address range "192.168.x.x" +ref: for (i, j) in product(list(range(256)), list(range(256))): + pass +Output: for i in range('192.168.x.x'):▁pass +bleu_metric: 7.916713848850043 +---------- +NL: loop through the IP address range "192.168.x.x" +ref: generator = iter_iprange('192.168.1.1', '192.168.255.255', step=1) +Output: for i in range('192.168.x.x'):▁pass +bleu_metric: 18.291291695991816 +---------- +NL: Sum the corresponding decimal values for binary values of each boolean element in list `x` +ref: sum(1 << i for i, b in enumerate(x) if b) +Output: ▁sum([int(x) for x in▁list(bool(x))]) +bleu_metric: 14.071964689317513 +---------- +NL: write multiple strings `line1`, `line2` and `line3` in one line in a file `target` +ref: target.write('%r\n%r\n%r\n' % (line1, line2, line3)) +Output: f.write(line1,▁line2 +▁line3.split(',')) +bleu_metric: 16.116608915070366 +---------- +NL: Convert list of lists `data` into a flat list +ref: [y for x in data for y in (x if isinstance(x, list) else [x])] +Output: [j for i in data for j in i] +bleu_metric: 9.344244386017191 +---------- +NL: Print new line character as `\n` in a string `foo\nbar` +ref: print('foo\nbar'.encode('string_escape')) +Output: """foo\nbar""".replace('\n') +bleu_metric: 32.49189152106538 +---------- +NL: remove last comma character ',' in string `s` +ref: """""".join(s.rsplit(',', 1)) +Output: s = s.replace(',', '') +bleu_metric: 24.4545047029932 +---------- +NL: calculate the mean of each element in array `x` with the element previous to it +ref: (x[1:] + x[:-1]) / 2 +Output: np.mean(x) +bleu_metric: 6.656592803413297 +---------- +NL: get an array of the mean of each two consecutive values in numpy array `x` +ref: x[:-1] + (x[1:] - x[:-1]) / 2 +Output: ▁numpy.array([[1, 2], [2, 4]]) +bleu_metric: 10.64536882179483 +---------- +NL: load data containing `utf-8` from file `new.txt` into numpy array `arr` +ref: arr = numpy.fromiter(codecs.open('new.txt', encoding='utf-8'), dtype='