Example #1
0
        private static void transform()
        {
            using (Pnyx p = new Pnyx())
            {
                p.read("C:/dev/asclepius/prod_import/BHC Patients from 1-1-15 thru 10-31-2018.csv");
                p.parseCsv(hasHeader: true);
                p.withColumns(p2 => p2.lineTransformer(new DateTransform
                {
                    formatSource = DateUtil.FORMAT_MDYYYY, formatDestination = DateUtil.FORMAT_ISO_8601_DATE
                }), 4, 5, 6);
                p.rowTransformerFunc(row =>
                {
                    String fullName = row[2];

                    Name name = NameUtil.parseFullName(fullName);
                    if (name == null)
                    {
                        return(null);
                    }

                    // Expands name into 4 columns
                    row = RowUtil.replaceColumn(row, 3, name.firstName, name.middleName, name.lastName, name.suffix);
                    return(row);
                });
                p.tee(p2 =>
                {
                    p2.removeColumns(7 + 3, 8 + 3, 9 + 3);                // plus 3 from name split above
                    p2.rowFilter(new RepeatFilter());
                    p2.write("C:/dev/asclepius/prod_import/bhc_discharges.csv");
                });
                p.widthColumns(9 + 3);                                  // plus 3 from name split above
                p.write("C:/dev/asclepius/prod_import/bhc_discharges_diagnosis.csv");
                p.process();
            }
        }
        public void explicitArgs(int argCount, int?readArg, int?writeArg, String error)
        {
            String inPath  = Path.Combine(TestUtil.findTestFileLocation(), "encoding", "psalm23.unix.ansi.txt");
            String outPath = Path.Combine(TestUtil.findTestOutputLocation(), "argsOutput", Guid.NewGuid() + ".txt");

            FileUtil.assureDirectoryStructExists(outPath);

            String[] args;
            switch (argCount)
            {
            default: args = new string[0]; break;

            case 1: args = new [] { inPath }; break;

            case 2: args = new [] { inPath, outPath }; break;

            case 3: args = new [] { inPath, outPath, "junk" }; break;
            }
            ArgsInputOutput numbered = new ArgsInputOutput(args);

            using (Pnyx p = new Pnyx())
            {
                p.setSettings(processOnDispose: false);
                p.setNumberedInputOutput(numbered);

                try
                {
                    if (readArg == null)
                    {
                        p.read(inPath);
                    }
                    else
                    {
                        p.readArg(readArg.Value);
                    }

                    if (writeArg == null)
                    {
                        p.write(outPath);
                    }
                    else
                    {
                        p.writeArg(writeArg.Value);
                    }

                    p.compile();

                    Assert.Null(error);
                }
                catch (Exception err)
                {
                    Assert.Equal(error, err.Message);
                    return;
                }

                p.process();
            }

            Assert.Null(TestUtil.binaryDiff(inPath, outPath));
        }
Example #3
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleFluent builder
        public static void builder()
        {
            using (var p = new Pnyx())
                p.readString("a,b,c,d").parseCsv().print("$4|$3|$2|$1").writeStdout();

            // outputs: d|c|b|a
        }
Example #4
0
        private static void ccmNames()
        {
            using (Pnyx p = new Pnyx())
            {
                p.read("C:/dev/asclepius/prod_import/ccm_names.csv");
                p.parseCsv(hasHeader: true);
                p.rowTransformerFunc(row =>
                {
                    String lastName = row[1];
                    Tuple <String, String> lastNameSuffix = NameUtil.parseSuffix(lastName);

                    if (lastNameSuffix.Item2 == null)
                    {
                        return(null);
                    }

                    // Expands name into 2 columns
                    row = RowUtil.replaceColumn(row, 2, lastNameSuffix.Item1, lastNameSuffix.Item2);
                    return(row);
                });
                p.rowTransformerFunc(row =>
                {
                    for (int i = 0; i < row.Count; i++)
                    {
                        row[i] = TextUtil.encodeSqlValue(row[i]);
                    }
                    return(row);
                });
                p.print("update bhc_patient_ccm set lastname=$2, suffix=$3 where patientid=$1;");
                p.write("C:/dev/asclepius/prod_import/ccm_names_update.sql");
                p.process();
            }
        }
Example #5
0
        protected void executeMethod(Pnyx p, String methodName, List <YamlScalarNode> parameterNodes)
        {
            List <MethodInfo> methodMatches = methods.Where(m => m.Name == methodName).ToList();
            MethodInfo        method        = methodMatches.FirstOrDefault(m => m.GetParameters().Length == parameterNodes.Count);

            if (method == null)
            {
                method = methodMatches.OrderByDescending(m => m.GetParameters().Length).FirstOrDefault();           // finds longest number of paramets
            }
            if (method == null)
            {
                throw new InvalidArgumentException("Pnyx method can not be found: {0}", methodName);
            }

            ParameterInfo[] methodParameters = method.GetParameters();
            ParameterInfo   multiParameter   = findParameterArray(methodParameters);

            if (parameterNodes.Count > methodParameters.Length && multiParameter == null)
            {
                throw new InvalidArgumentException("Too many parameters {0} specified for Pnyx method '{1}', which only has {2} parameters", parameterNodes.Count, methodName, methodParameters.Length);
            }

            // Checks for minimum size
            int requiredParameters = methodParameters.Count(pi => !pi.HasDefaultValue);

            if (parameterNodes.Count < requiredParameters + (multiParameter != null ? -1 : 0))
            {
                throw new InvalidArgumentException("Too few parameters {0} specified for Pnyx method '{1}', which only has {2} required parameters", parameterNodes.Count, methodName, requiredParameters);
            }

            // Builds parameter list with defaults
            Object[] parameters = new Object[methodParameters.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfo current = methodParameters[i];

                if (current == multiParameter)
                {
                    parameters[i] = processMultiParameters(multiParameter, i == 0 ? parameterNodes : parameterNodes.Skip(i));
                }
                else if (i < parameterNodes.Count)
                {
                    parameters[i] = processScalarParameter(current, parameterNodes[i]);
                }
                else
                {
                    parameters[i] = current.DefaultValue;
                }
            }

            try
            {
                // Runs method
                method.Invoke(p, parameters);
            }
            catch (TargetInvocationException err)
            {
                throw err.InnerException;
            }
        }
Example #6
0
        public void parseCode(Pnyx p, String source, bool compilePnyx = true)
        {
            // Compiles
            Script script = CSharpScript.Create(source,
                                                globalsType: typeof(Pnyx),
                                                options: ScriptOptions.Default.WithReferences(typeof(Pnyx).Assembly)
                                                );

            Task <ScriptState> parseTask = script.RunAsync(p);

            if (parseTask.IsCompletedSuccessfully)
            {
                if (p.state != FluentState.Compiled && compilePnyx)
                {
                    p.compile();                            // builds processor
                }
            }
            else
            {
                if (parseTask.Exception != null)
                {
                    throw new IllegalStateException("Script failed to run with error: {0}", parseTask.Exception.Message);
                }

                throw new IllegalStateException("Script failed to run with task in state: {0}", parseTask.Status.ToString());
            }
        }
Example #7
0
        private static void transform()
        {
            using (Pnyx p = new Pnyx())
            {
                p.setSettings(outputNewline: "\n");

                p.read("C:/dev/asclepius/prod_import/bhc_procedures.csv");
                p.parseCsv(hasHeader: true);
                p.withColumns(p2 => p2.lineTransformer(new DateTransform
                {
                    formatSource = DateUtil.FORMAT_MDYYYY, formatDestination = DateUtil.FORMAT_ISO_8601_DATE
                }), 12);
                p.rowTransformer(new DateFixer());
                p.tee(p2 =>
                {
                    p2.removeColumns(7, 8, 9, 10, 11);
                    p2.withColumns(p3 => p3.rowFilter(new RepeatFilter()), 1, 2, 7);
                    p2.write("C:/dev/asclepius/prod_import/bhc_procedure_base.csv");
                });
                p.removeColumns(3, 4, 5, 6);
                p.rowTransformer(new SequenceFixer());
                p.write("C:/dev/asclepius/prod_import/bhc_procedure_diagnosis.csv");
                p.process();
            }
        }
Example #8
0
        public void parseBlock(Pnyx p, YamlSequenceNode block)
        {
            foreach (YamlNode node in block)
            {
                if (node.NodeType != YamlNodeType.Mapping)
                {
                    throw new InvalidArgumentException("Expected YAML mapping for command/value pair, but found: {0}", node.NodeType.ToString());
                }

                YamlMappingNode commandNode = (YamlMappingNode)node;
                if (commandNode.Children.Count != 1)
                {
                    throw new InvalidArgumentException("Expected YAML mapping with 1 command/value pair, but found: {0}", commandNode.Children.Count);
                }

                KeyValuePair <YamlNode, YamlNode> commandPair = commandNode.Children.First();
                if (commandPair.Key.NodeType != YamlNodeType.Scalar)
                {
                    throw new InvalidArgumentException("Expected YAML scalar for commandName, but found: {0}", commandPair.Key.NodeType.ToString());
                }

                YamlScalarNode commandName = (YamlScalarNode)commandPair.Key;
                switch (commandPair.Value.NodeType)
                {
                case YamlNodeType.Scalar: parseScalarNode(p, commandName, (YamlScalarNode)commandPair.Value); break;

                case YamlNodeType.Sequence: parseSequenceNode(p, commandName, (YamlSequenceNode)commandPair.Value); break;

                case YamlNodeType.Mapping: parseMappingNode(p, commandName, (YamlMappingNode)commandPair.Value); break;

                default: throw new InvalidArgumentException("YAML node isn't currently supported '{0}' for command values", commandPair.Value.NodeType.ToString());
                }
            }
        }
Example #9
0
        public void columnFilter()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.parseCsv();
                p.columnFilter(3, new Grep {
                    textToFind = "titan", caseSensitive = false
                });
                actual = p.processToString();
            }

            Assert.Equal(PLANETS_GODS_TITANS, actual);

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.parseCsv();
                p.columnFilter(1, new Grep {
                    textToFind = "titan", caseSensitive = false
                });
                actual = p.processToString();
            }

            Assert.Equal("", actual);
        }
Example #10
0
        public void tailLine()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.tailStream(p2 => p2.readString(MAGNA_CARTA), 2);
                actual = p.processToString();
            }

            const String expected =
                @"Matthew Fitz Herbert, Thomas Basset, Alan Basset, Philip Daubeny, Robert de Roppeley,
John Marshal, John Fitz Hugh, and other loyal subjects:";

            Assert.Equal(expected, actual);


            // Uses buffer instead of manipulating the stream
            using (Pnyx p = new Pnyx())
            {
                p.readString(MAGNA_CARTA);
                p.tail(2);
                actual = p.processToString();
            }
            Assert.Equal(expected, actual);
        }
Example #11
0
        public void tailStreamRow()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.tailStream(p2 => p2.readString(PLANETS_GODS), 2);
                p.parseCsv();
                actual = p.processToString();
            }

            const String expected =
                @"Uranus,Uranus,""Father of the Titans""
Zeus,Jupiter,""Sky god, supreme ruler of the Olympians""
";

            Assert.Equal(expected, actual);


            // Uses buffer instead of manipulating the stream
            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS);
                p.parseCsv();
                p.tail(2);
                actual = p.processToString();
            }
            Assert.Equal(expected, actual);
        }
Example #12
0
        public void teeLine()
        {
            StringBuilder capture = new StringBuilder();
            String        actualA;

            Pnyx p    = new Pnyx();
            Pnyx teeP = null;

            using (p)
            {
                p.readString(PLANETS_GODS_TITANS);
                p.tee(pn =>
                {
                    teeP = pn;
                    pn.captureText(capture);
                });
                actualA = p.processToString();
            }
            String actualB = capture.ToString();

            Assert.Equal(actualA, PLANETS_GODS_TITANS);
            Assert.Equal(actualB, PLANETS_GODS_TITANS);
            Assert.Equal(FluentState.Disposed, p.state);
            Assert.Equal(FluentState.Disposed, teeP.state);
        }
Example #13
0
        public void teeRow()
        {
            MemoryStream capture = new MemoryStream();
            String       actualA;

            Pnyx p    = new Pnyx();
            Pnyx teeP = null;

            using (p)
            {
                p.readString(PLANETS_GODS_FORMAT_ISSUES);
                p.parseCsv(strict: false);
                p.tee(pn =>
                {
                    teeP = pn;
                    pn.writeStream(capture);
                });
                actualA = p.processToString();
            }
            String actualB = p.streamInformation.getOutputEncoding().GetString(capture.ToArray());

            Assert.Equal(actualA, PLANETS_GODS);
            Assert.Equal(actualB, PLANETS_GODS);
            Assert.Equal(FluentState.Disposed, p.state);
            Assert.Equal(FluentState.Disposed, teeP.state);
        }
Example #14
0
        public void verifyColumnSorting()
        {
            const String input =
                @"a,a,9
a,c,7
a,b,5
c,a,1
a,d,8
";
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.parseCsv();
                p.sort(columnNumbers: new [] { 1, 3 });
                actual = p.processToString();
            }

            const String expect =
                @"a,b,5
a,c,7
a,d,8
a,a,9
c,a,1
";

            Assert.Equal(expect, actual);
        }
Example #15
0
 public void cat()
 {
     using (Pnyx p = new Pnyx())
     {
         p.readString(PLANETS_GODS);
         Assert.Throws <IllegalStateException>(() => p.readString(PLANETS_GODS));
     }
 }
Example #16
0
 public void Dispose()
 {
     if (p != null)
     {
         p.Dispose();
         p = null;
     }
 }
Example #17
0
 public static void minimum()
 {
     using (Pnyx p = new Pnyx())
     {
         p.readString("Minimum");
         p.writeStdout();
     }
     // outputs: Minimum
 }
Example #18
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleSettings factory
        public static void factory()
        {
            SettingsHome.settingsFactory = new CustomFactory();

            using (Pnyx p = new Pnyx())
            {
                // uses custom settings
            }
        }
Example #19
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleFluent pnyxMethods
        public static void pnyxMethods()
        {
            MethodInfo[] methods = typeof(Pnyx).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            String names = String.Join("\n", methods.Select(mi => mi.Name));

            using (var p = new Pnyx())
                p.readString(names).write(@"c:\dev\ee.txt");
        }
Example #20
0
 public void readImproperState()
 {
     using (Pnyx p = new Pnyx())
     {
         p.readString(EARTH);
         p.sed("Ter.*", "Forma", "g");
         Assert.Throws <IllegalStateException>(() => p.readString(EARTH));
     }
 }
Example #21
0
 // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleOutput specificEncoding
 public static void specificEncoding()
 {
     using (Pnyx p = new Pnyx())
     {
         p.setSettings(outputEncoding: Encoding.UTF8);
         p.read("a\nb\nc");
         p.write("myfile.txt");
     }
     // Writes a UTF-8 file regardless of source
 }
Example #22
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleOutput setEndLine
        public static void setEndLine()
        {
            CustomEndLine processor = new CustomEndLine();

            using (Pnyx p = new Pnyx())
            {
                p.readString("a\nb\nc");
                p.endLine(processor);
            }
        }
 public void headerNamesError(String message, params Object[] columnNumbersAndNames)
 {
     using (Pnyx p = new Pnyx())
     {
         p.readString("bb");
         p.parseCsv(hasHeader: true);
         InvalidArgumentException exception = Assert.Throws <InvalidArgumentException>(() => p.headerNames(columnNumbersAndNames));
         Assert.Equal(message, exception.Message);
     }
 }
Example #24
0
 // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleInput specificEncoding
 public static void specificEncoding()
 {
     using (Pnyx p = new Pnyx())
     {
         p.setSettings(defaultEncoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: false);
         p.read("myfile.txt");
         p.writeStdout();
     }
     // Reads a UTF-8 file regardless of BOM (or lack thereof)
 }
Example #25
0
        protected void parseScalarNode(Pnyx p, YamlScalarNode name, YamlScalarNode value)
        {
            List <YamlScalarNode> parameterNodes = new List <YamlScalarNode>();

            if (value.Value != "")
            {
                parameterNodes.Add(value);
            }

            executeMethod(p, name.Value, parameterNodes);
        }
Example #26
0
 // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleProcessorChain sedShim
 public static void sedShim()
 {
     using (Pnyx p = new Pnyx())
     {
         p.readString("CSV,INPUT!,\"Go, Pnyx Go\"");
         p.parseCsv();
         p.sed("[,!]", "_", "g");
         p.writeStdout();
     }
     // outputs: CSV,INPUT_,"Go_ Pnyx Go"
 }
Example #27
0
        // pnyx -e=documentation pncs.cmd.examples.documentation.library.ExampleInput newlines
        public static void newlines()
        {
            const String input = "1\n2\r\n3\r4";

            using (Pnyx p = new Pnyx())
            {
                p.readString(input);
                p.writeStdout();
            }
            // outputs: 1\n2\n3\n4
        }
Example #28
0
        public void inOut()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(MAGNA_CARTA);
                actual = p.processToString();
            }

            Assert.Equal(MAGNA_CARTA, actual);
        }
Example #29
0
        public void asCsv()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.asCsv(pn => pn.readString(PLANETS_GODS_FORMAT_ISSUES), strict: false);
                actual = p.processToString();
            }

            Assert.Equal(PLANETS_GODS, actual);
        }
Example #30
0
        public void csvInOutVariant()
        {
            String actual;

            using (Pnyx p = new Pnyx())
            {
                p.readString(PLANETS_GODS_FORMAT_ISSUES);
                actual = p.processToString((pnyx, stream) => pnyx.writeCsvStream(stream, strict: false));
            }

            Assert.Equal(PLANETS_GODS, actual);
        }