Skip to content

Commit 6f46706

Browse files
committed
Fix command line
1 parent 8d73f11 commit 6f46706

File tree

12 files changed

+341
-53
lines changed

12 files changed

+341
-53
lines changed

bin/pseudo-python

Lines changed: 0 additions & 50 deletions
This file was deleted.

examples/football.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Collections.Generic;
6+
7+
public class Result
8+
{
9+
private readonly string host;
10+
public string Host { get { return host; } }
11+
12+
private readonly string away;
13+
public string Away { get { return away; } }
14+
15+
private readonly int[] goals;
16+
public int[] Goals { get { return goals; } }
17+
18+
public Result(string host, string away, int[] goals)
19+
{
20+
this.host = host;
21+
this.away = away;
22+
this.goals = goals;
23+
}
24+
}
25+
26+
public class Program
27+
{
28+
static List<Result> LoadResults(string filename)
29+
{
30+
var raw = File.ReadAllText(filename);
31+
var lines = raw.Split('\n');
32+
return lines
33+
.Where(line => line.Length != 0)
34+
.Select(line => ParseResult(line))
35+
.ToList();
36+
}
37+
38+
static Result ParseResult(string line)
39+
{
40+
var awayIndex = line.IndexOf(" - ") + 3;
41+
var resultIndex = line.IndexOf(" ", awayIndex) + 1;
42+
var goals = line.Substring(resultIndex).Split(':');
43+
return new Result(line.Substring(0, awayIndex - 3), line.Substring(awayIndex, resultIndex - 1 - awayIndex), new[] { Int32.Parse(goals[0]), Int32.Parse(goals[1]) });
44+
}
45+
46+
static int CalculatePoints(List<Result> results, string team)
47+
{
48+
return results.Aggregate(0, (memo, result) => memo + ResultPoints(team, result));
49+
}
50+
51+
static int ResultPoints(string team, Result result)
52+
{
53+
if (result.Host == team && result.Goals[0] > result.Goals[1] || result.Away == team && result.Goals[0] < result.Goals[1])
54+
{
55+
return 3;
56+
}
57+
else if (result.Goals[0] == result.Goals[1] && (result.Host == team || result.Away == team))
58+
{
59+
return 1;
60+
}
61+
else
62+
{
63+
return 0;
64+
}
65+
}
66+
67+
public static void Main(string[] args)
68+
{
69+
if (args.Length < 2)
70+
{
71+
Console.WriteLine("usage: football <stats-file> <team>");
72+
}
73+
else
74+
{
75+
var results = LoadResults(args[0]);
76+
Console.WriteLine(CalculatePoints(results, args[1]));
77+
}
78+
}
79+
}

examples/football.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"strconv"
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
)
10+
11+
func LoadResults(filename string) []Result {
12+
_contents, _ := ioutil.ReadFile(filename)
13+
raw := string(_contents)
14+
lines := strings.Split(raw, "\n")
15+
var _results []Result
16+
for _, line := range lines {
17+
if len(line) > 0 {
18+
_results = append(_results, ParseResult(line))
19+
}
20+
}
21+
return _results
22+
23+
}
24+
25+
func ParseResult(line string) *Result {
26+
awayIndex := strings.Index(line, " - ") + 3
27+
resultIndex := awayIndex + strings.Index(line[awayIndex:], " ") + 1
28+
goals := strings.Split(line[resultIndex:], ":")
29+
_int, _ := strconv.Atoi(goals[0])
30+
_int1, _ := strconv.Atoi(goals[1])
31+
return Result{line[:awayIndex - 3], line[awayIndex:resultIndex - 1], [...]int{_int, _int1}}
32+
}
33+
34+
func CalculatePoints(results []Result, team string) int {
35+
accumulator := 0
36+
for _, result := range results {
37+
accumulator += ResultPoints(team, result)
38+
}
39+
40+
return accumulator
41+
}
42+
43+
func ResultPoints(team string, result Result) int {
44+
if result.Host == team && result.Goals[0] > result.Goals[1] || result.Away == team && result.Goals[0] < result.Goals[1] {
45+
return 3
46+
} else if result.Goals[0] == result.Goals[1] && (result.Host == team || result.Away == team) {
47+
return 1
48+
} else {
49+
return 0
50+
}
51+
}
52+
53+
type Result struct {
54+
Host string
55+
Away string
56+
Goals [2]int
57+
}
58+
59+
func main() {
60+
if len(os.Args) < 3 {
61+
fmt.Println("usage: football <stats-file> <team>")
62+
} else {
63+
results := LoadResults(os.Args[1])
64+
fmt.Println(CalculatePoints(results, os.Args[2]))
65+
}
66+
}

examples/football.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var _ = require('lodash');
2+
var fs = require('fs');
3+
function load_results(filename) {
4+
var raw = fs.readFileSync(filename, 'utf8');
5+
var lines = raw.split('\n');
6+
return _.filter(lines, function (line) {
7+
return line;
8+
}).map(function (line) {
9+
return parse_result(line);
10+
});
11+
}
12+
13+
function parse_result(line) {
14+
var away_index = line.search(' - ') + 3;
15+
var result_index = away_index + line.slice(away_index).search(' ') + 1;
16+
var goals = line.slice(result_index).split(':');
17+
return [line.slice(0, away_index - 3), line.slice(away_index, result_index - 1), [parseInt(goals[0]), parseInt(goals[1])]];
18+
}
19+
20+
function calculate_points(results, team) {
21+
return _.reduce(results, function (memo, result) {
22+
return memo + result_points(team, result[0], result[1], result[2]);
23+
}, 0);
24+
}
25+
26+
function result_points(team, host, away, goals) {
27+
if (host == team && goals[0] > goals[1] || away == team && goals[0] < goals[1]) {
28+
return 3;
29+
} else if (goals[0] == goals[1] && (host == team || away == team)) {
30+
return 1;
31+
} else {
32+
return 0;
33+
}
34+
}
35+
36+
if (process.argv.length < 4) {
37+
console.log('usage: football <stats-file> <team>');
38+
} else {
39+
var results = load_results(process.argv[2]);
40+
console.log(calculate_points(results, process.argv[3]));
41+
}
42+

examples/football.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def result_points(team, host, away, goals):
2929
else:
3030
results = load_results(sys.argv[1])
3131
# print(result_points(sys.argv[2], *results[0]))
32-
print(calculate_points(results, sys.argv[2]))
32+
print(calculate_points(results, sys.argv[2]))

examples/football.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def load_results(filename)
2+
raw = File.read(filename)
3+
lines = raw.split("\n")
4+
lines.select { |line| !line.empty? }.map { |line| parse_result(line) }
5+
end
6+
7+
def parse_result(line)
8+
away_index = line.index(' - ') + 3
9+
result_index = line.index(' ', away_index) + 1
10+
goals = line[result_index..-1].split(':')
11+
[line[0...away_index - 3], line[away_index...result_index - 1], [goals[0].to_i, goals[1].to_i]]
12+
end
13+
14+
def calculate_points(results, team)
15+
results.reduce(0) { |memo, result| memo + result_points(team, result[0], result[1], result[2]) }
16+
end
17+
18+
def result_points(team, host, away, goals)
19+
if host == team && goals[0] > goals[1] || away == team && goals[0] < goals[1]
20+
3
21+
elsif goals[0] == goals[1] && (host == team || away == team)
22+
1
23+
else
24+
0
25+
end
26+
27+
end
28+
29+
if ARGV.length < 2
30+
puts 'usage: football <stats-file> <team>'
31+
else
32+
results = load_results(ARGV[0])
33+
puts calculate_points(results, ARGV[1])
34+
end
35+

pseudo_python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
import pseudo_python.ast_translator
33

44
def translate(source):
5-
return pseudo_python.ast_translator.ASTTranslator(pseudo_python.parser.parse(source), source).translate()
5+
return pseudo_python.ast_translator.ASTTranslator(pseudo_python.parser.parse(source), source).translate()

pseudo_python/main.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import pseudo_python
5+
import pseudo_python.errors
6+
import yaml
7+
import pseudo
8+
import pseudo.errors
9+
from colorama import init
10+
from termcolor import colored
11+
12+
USAGE = '''
13+
pseudo-python <input-filename.py> [<output-filename> / <language>]
14+
15+
where if you omit <language>, pseudo-python will generate a
16+
<input-filename.pseudo.yaml> file with serialized ast
17+
18+
if <output-filename> is provided, <language> will be extracted from
19+
the extension
20+
21+
it can be:
22+
py / python
23+
rb / ruby
24+
js / javascript
25+
cs / csharp
26+
go
27+
28+
examples:
29+
pseudo-python a.py # generates a.pseudo.yaml
30+
pseudo-python z.py o.rb # generates a ruby translation in o.rb
31+
'''
32+
33+
def main():
34+
if len(sys.argv) == 1:
35+
print(USAGE)
36+
return
37+
38+
filename = sys.argv[1]
39+
with open(filename, 'r') as f:
40+
source = f.read()
41+
base = filename.partition('.')[0]
42+
try:
43+
if len(sys.argv) == 2:
44+
yaml.Dumper.ignore_aliases = lambda *args : True
45+
clj = yaml.dump(pseudo_python.translate(source))
46+
with open('%s.pseudo.yaml' % base, 'w') as f:
47+
f.write(clj)
48+
print(colored('OK\nsaved pseudo ast as %s.pseudo.yaml' % base, 'green'))
49+
else:
50+
arg = sys.argv[2]
51+
if '.' in arg:
52+
base, _, language = arg.rpartition('.')
53+
else:
54+
language = arg
55+
if language not in pseudo.SUPPORTED_FORMATS:
56+
print(colored('%s is not supported' % language, 'red'))
57+
exit(1)
58+
if '%s.%s' % (base, pseudo.FILE_EXTENSIONS[language]) == filename:
59+
print(colored('this would overwrite the input file, please choose another name', 'red'))
60+
exit(1)
61+
node = pseudo_python.translate(source)
62+
output = pseudo.generate(node, language)
63+
with open('%s.%s' % (base, pseudo.FILE_EXTENSIONS[language]), 'w') as f:
64+
f.write(output)
65+
print(colored('OK\nsaved as %s.%s' % (base, pseudo.FILE_EXTENSIONS[language]), 'green'))
66+
except pseudo_python.errors.PseudoError as e:
67+
print(colored(e, 'red'))
68+
if e.suggestions:
69+
print(colored(e.suggestions, 'green'))
70+
if e.right:
71+
print(colored('\nright:\n%s' % e.right, 'green'))
72+
if e.wrong:
73+
print(colored('\nwrong:\n%s' % e.wrong, 'red'))
74+
except pseudo.errors.PseudoError as e:
75+
print(colored('Pseudo error:\n%s' % e, 'red'))
76+
77+
if __name__ == '__main__':
78+
main()

pseudo_python/tests/assign.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PyYAML==3.11
22
colorama
33
termcolor
4+
pseudo>=0.2.3

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

0 commit comments

Comments
 (0)