-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenmodel.pl
36 lines (28 loc) · 850 Bytes
/
genmodel.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use strict;
my $file = shift @ARGV;
open my $fh, '<:utf8', $file or die "Could not open $file: $!";
my $line = <$fh>;
chomp $line;
my @fields = split /\t/, $line;
my (@field_lengths, @all_digits);
for (1..scalar(@fields)) {
push @field_lengths, 0;
push @all_digits, 1;
}
while ($line = <$fh>) {
chomp $line;
my @f = split /\t/, $line;
for (my $i = 0; $i < scalar(@f); $i++) {
my $len = length($f[$i]);
$field_lengths[$i] = $len if $len > $field_lengths[$i];
$all_digits[$i] = 0 if $all_digits[$i] && $f[$i] =~ m/[^\d]/;
}
}
for (my $i = 0; $i < scalar(@fields); $i++) {
if ($all_digits[$i]) {
print ' ' . $fields[$i] . ' = models.IntegerField()'. "\n";
}
else {
print ' ' . $fields[$i] . ' = models.CharField(max_length=' . $field_lengths[$i] . ")\n";
}
}