ruby

How to make each method in Python?

How to replicate Ruby’s "each" method in Python? Question: How can I make method each in Python like its in Ruby? Is that possible, am really do! In Ruby its looks like: %w[1 2 3 4].each { |i| puts i} In Python we have for: for i in [1, 2, 3]: print(i) So, I want …

Total answers: 1

How can I recreate Ruby's sort_by {rand} in python?

How can I recreate Ruby's sort_by {rand} in python? Question: I have a line of code where can sort an array into random order: someArray.sort_by {rand} so in python, how can I convert to it to python code? Asked By: hanan || Source Answers: Maybe you are looking for this: import random l = [1, …

Total answers: 2

Compute variable expressions in mustache templates: what should we get?

Compute variable expressions in mustache templates: what should we get? Question: Given these hash and Mustache template: Hash: { ‘a’: 3 } Template: "This is a+2: {{a+2}}" Ruby and Python give me different outputs: In ruby, I get: /usr/lib/ruby/gems/3.0.0/gems/mustache-1.1.1/lib/mustache/parser.rb:286:in `error’: Unclosed tag (Mustache::Parser::SyntaxError) Line 1 {{a+2}} In python, just empty string Who is right? What …

Total answers: 1

How to use Faktory with a ruby job and a python worker?

How to use Faktory with a ruby job and a python worker? Question: I want to use a ruby script to submit jobs to a faktory server (https://github.com/contribsys/faktory/wiki) to launch a python worker. Faktory should be perfect for that being language agnostic but the documentation covers only the ruby only (job and worker) or python …

Total answers: 1

Why does integer division round down in many scripting languages?

Why does integer division round down in many scripting languages? Question: In the languages I have tested, – (x div y ) is not equal to -x div y; I have tested // in Python, / in Ruby, div in Perl 6; C has a similar behavior. That behavior is usually according to spec, since …

Total answers: 6

Is there a python equivalent for RSpec to do TDD?

Is there a python equivalent for RSpec to do TDD? Question: I’m looking for a test framework like Ruby’s RSpec to do test driven development in Python. The advantage of a framework like RSpec is that it offers a DSL that lends itself well to TDD. First you describe the test in english, and then …

Total answers: 4

Interactive CLI packages – checkboxes & selection

Interactive CLI packages – checkboxes & selection Question: I am trying to learn more about making some cool CLI interfaces to provide the options for some local scripts. By digging into the source of the yeoman-generator I was able to come across inquirer for Node, which is how I learned that it was possible. (Example …

Total answers: 2

Print out all combinations of elements in an array of arrays

Print out all combinations of elements in an array of arrays Question: Say you had the following x = [[1,2,3], [4,5,6], [7,8,9]] What’s the best way, say in python or ruby, to print out all combinations of the inner elements? So the result would look like: 147, 148, 149, 157, 158, 159, 167, … This …

Total answers: 3

Converting epoch time with milliseconds to datetime

Converting epoch time with milliseconds to datetime Question: I have used a ruby script to convert iso time stamp to epoch, the files that I am parsing has following time stamp structure: 2009-03-08T00:27:31.807 Since I want to keep milliseconds I used following ruby code to convert it to epoch time: irb(main):010:0> DateTime.parse(‘2009-03-08T00:27:31.807’).strftime(“%Q”) => “1236472051807” But …

Total answers: 3