Latest Software Development Posts

Dear Jag_1,

I have seen the effort you put into having the result you wanted. But you are not doing it rightly.

You can, like you are doing use regex to pick what you need and modify the file to want you need.

Since, this is your first post on this platform. I will show you one to to achieve what you want to do.

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $hash = {};
my $class_name;

while (<DATA>) {

    # remove all the spaces at the begin and the end of each line
    s/^\s+|\s+$//g;

    if ( /Name.+/ ... /}/ ) {
        my $line = [ split /\s+/ => $_, 2 ];    # split each line
        if ( $line->[0] eq "fruitName" ) {      # at every instance of fruitName
            $class_name = $line->[1];           # get a new fruit clas
        }
        else {
            next if /Id|Name|fruitName|\}/;     # don't include this in our hash
                 # modify to remove the word fruit from each line
            ( my $sub_class = $line->[0] ) =~ s/fruit(.+)$/$1/ig;

            # get the wanted values
            push @{ $hash->{$class_name} } => { $sub_class => $line->[1] };
        }
    }
}

print Dumper $hash;

# to print out. Not in Dumper style.
sub {
    my $table = shift;
    for my $fruit ( keys %$table ) {
        print $fruit, $/;
        map { print "\t", join( "\t" => %$_ ), $/ } @{ $table->{$fruit} };
    }
  }
  ->($hash);

__DATA__
{
date 14/07/2016
time 11:15
end 11:20
total 30
No    "FRUITS"
Fruit_class
    {
    Name    "fruit 1" ...
pty 867

Additionally, I forgot the sorting element.

For an array of hashes you'll notice that if you try to sort directly the comparison will fail:

[{name: "Ringo"}, {name: "John"}, {name: "Paul"}, {name: "George"}].sort
ArgumentError: comparison of Hash with Hash failed

You need to tell Ruby how to sort, using #sort_by:

[{name: "Ringo"}, {name: "John"}, {name: "Paul"}, {name: "George"}].sort_by{|b| b[:name]}
=> [{:name=>"George"}, {:name=>"John"}, {:name=>"Paul"}, {:name=>"Ringo"}]

If you go down the class route you would need to implement the comparison method, <=>, as that's what is used to compare elements within sort. For the time being I'd stick to using a hash, it works nicely for this type of puzzle.

pty 867

You have made a decent start. In addition to looping you need to think about how you might store the user-entered results.

In order to loop you're probably best using the #times method of an integer, this means you'll have to convert your gemstoneNumber to an integer before looping.

[1] pry(main)> quantity = "2".to_i    # simulating the gets which always retuns a string
=> 2
[2] pry(main)> quantity.times do |i|
[2] pry(main)*   puts i
[2] pry(main)* end
0
1

Secondly, how do you want to store your data? You could use a hash per gemstone and store the records in an array.

gemstones = []
gemstones.push({name: "Sapphire", color: "Blue", price: 24.0})
gemstones.push({name: "Emerald", color: "Green", price: 21.0})

Or you could create a new Gemstone class with attribute readers and store an array of these:

class Gemstone
  attr_reader :name, :colour, :price

  def initialize(name, colour, price)
    @name, @colour, @price = name, colour, price
  end

end

The beauty of this approach is that you can create a to_s method in your class and call it to print out your "You entered..." line.

Hopefully some of this made sense, but you can get the result you want by looping based on the quantity using times and storing the data in an array. If you get stuck post your updated code and I'll have another look. Good luck.

This is a program that ask input from the user about the characteristics of a gemstone then prints to the screen. Things like color, price and name. I have written it to an extend where the user enters all this and prints them out. Am now stuck where I should loop and enable the user to enter any number of gemstones. Like if he/she enters 3 gemstones then it should loop and allow the user to enter the characteristics of the 3 gemstone types. I would also like to sort the resulting output of gemstone names in alphabetical order. Appreciated

class GemStones
  # input variables
  name = ""
  color = ""
  price = 0
  gemstoneNumber = 0

  # output variable
  gemstoneNumber = 0

  # processing
  print "How many gemstones do you want to enter? "
  gemstoneNumber = gets

  print "What is the name of the gemstone? "
  name = gets

  print "What is the color of the gemstone? "
  color = gets

  print "What is the price of the gemstone? "
  price = gets

  puts " You entered #{gemstoneNumber} The name is #{name}, the color is #{color} and price is
  $ #{price}"

end
pty 867

Excluding your aggregate columns you need to group by all you select by. That's just the way it works.

interesting
I only just skimmed over it, but it uses all standard library design principals, and doesn't really mention using pipes for syncing, but yeah, I'll give it a look through a few times over.

I think I might be a little more advance than it, cause I'll be using both multiprocessing and multithreading where appropriate ;)

my API has been in design since probably around 2013-2014, and I've learned alot within that time to now :)
going back and re-looking at stuff is always good though.

I want to share a small resource that I used years ago and due to your question I went and checked out again. It now includes some writing about Python! For your library. I consider this a must have.

Go get it at http://greenteapress.com/semaphores/LittleBookOfSemaphores.pdf

if anyone else wants to chime in, I do still need this.
I've given up looking for a solution for a time as I'm now working on another section of my API...
but I do intend to get back to this, and still need this method to work.

heck, at the very least provide info, cause I can't seem to find any...
DDG sucks here, and Google's results have been questionable...
Nullege is down, and GitHub just provides 100 pages of copies of subprocess and multiprocessing...
meanwhile other source engines hardly provide good results.

so yeah... I'll bother with this later...
I'm not blowing my brains out on it any longer...
I've already spent too much time on it. (take the discussion span of this post and add 2 days)

Hi,

I Have below table

I want all rows with all coloumn from below table where ActCode is 1162828 and sum of DocAmount for DocNo. i.e where the docNo is repeated i want only one row with sum of amount of all repeated rows.

**

"Select *, Sum (DocAmount)  from TblPurLedTranData  whre TblPurLedtranData.ActCode='" & 1162828 & "' Groupby DocNo "

**

PosDate     DocDate     DocNo         DocAmount     DueDate    Status       Period     DocType    FinYear       Chan          ActCode
10/10/2018  12/10/2018  369896       36980          11/11/2018  INV         5          PUST         2018        PUST        1243684   
10/10/2018  12/12/2018  665          36000          11/01/2019  INV         5           CR          2018       PUST       1162828   
10/10/2018  10/05/2018  3620         25000          09/06/2018  INV         5           INV         2018        PUST        1162828   
10/10/2018  10/05/2018  3620         25000          09/06/2018  INV         5           INV         2018        PUST        1162828   
10/10/2018  10/05/2018  3620         -69800         09/06/2018  INV         5           INV         2018        PUST        1162828   
10/10/2018  10/05/2018  6500         10200          09/06/2018  INV         5           INV         2018        PUST        1162828   
10/10/2018  10/05/2018  9988         35600          09/06/2018  INV         5           INV         2018        PUST        1162828   

Thanks

I've never found IP communication to be slow

you're probably smart and not using python like I am ;)

I needed my systems to be agnostic about the OS.

there's not much I need to worry about
all I really need is the process and pipe creation mechanics for each OS and I can make a simple class that manages everything.

everything in python has the extremely bad habit of extreme abstraction, just look at threading, subprocess, and multiprocessing...
those modules could do the exact same thing and be written with FAR less code.

and then there's modules like watchdog which brutaly abstract these much MUCH farther.
and here I've written (still working on) a single file watcher module that's not even 1000 lines.
(yes this is also part of my API)

to put it short, I'm trying to use "good python", which nobody uses:

  • performative
  • secure
  • memory friendly
  • readable (if you understand how python's mechanics work)

but anyways

not engaging or attempting to tackle your IPC woes.

that's fine, thanks for the advice ;)
I do plan to use sockets, but indirectly to pipes whenever a network session occures.

My second method. Not as scalable is to pass a boatload of information via a file. I've used the IP methods and files for years for communications among threads or apps. The problem with IPC is as you noted but for me I needed my systems to be agnostic about the OS. So once the IP or file method is known the apps can be on almost any OS or spread across a collection of computers without being locked to any specific OS.

Be aware I am sharing here and not engaging or attempting to tackle your IPC woes.

PS. Added with edit. For me I've never found IP communication to be slow. Since it can be local, it happens without any traversal to the external network.

I don't always wanna be 100% open to the network
plus sockets are super slow as demonstraited by IDLE
(though I could work my magic on that and do things more appropriately rather than use an RPC proxy interface)

I'm going to write "This is why I stopped using pipes and started using sockets." Also, with sockets I can scale across computers in the network.

Tcll commented: it's a good suggestion, but not what I'm looking to achieve ;) +4

so just to show exactly what I'm trying to do (this code hangs):

# -*- coding: utf-8 -*-
from _winapi import CreatePipe, CreateProcess, CloseHandle
from msvcrt import open_osfhandle
from nt import getpid

# this is just a mock-up path to a local interpreter, since __file__ doesn't include the drive letter on wine
CWD = 'Z:%s'%__file__.replace('test.py','').replace('\\','/')
exec_path = '%sapp/'%CWD
executable = '%spython.exe'%exec_path

crhandle, pwhandle = CreatePipe(None, 0) # parent -> child (input pipe)
prhandle, cwhandle = CreatePipe(None, 0) # child -> parent (output pipe)

subprocess_code = '''
from _winapi import OpenProcess, DuplicateHandle, GetCurrentProcess, CloseHandle
from _winapi import PROCESS_DUP_HANDLE, DUPLICATE_SAME_ACCESS, DUPLICATE_CLOSE_SOURCE
from msvcrt import open_osfhandle
import sys
sys.path = ['.\\DLLs','%s'] # because -I doesn't remove all C:/PythonXX paths (where this interpreter is in ./app/)

source_process_handle = OpenProcess(PROCESS_DUP_HANDLE, False, %s)
try:
    rhandle = DuplicateHandle( source_process_handle, %s, GetCurrentProcess(), 0, False, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)
    from_parent = open( open_osfhandle( rhandle, 0 ), 'rb', closefd=True )

    whandle = DuplicateHandle( source_process_handle, %s, GetCurrentProcess(), 0, False, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)
    to_parent = open( open_osfhandle( whandle, 0 ), 'wb', closefd=True )

    feedback = from_parent.read() # I'd like to run this in a while loop

    to_parent.write(b'Parent told me to say ' + feedback)

    to_parent.close()
    from_parent.close()
    sys.exit(0)
finally:
    CloseHandle(source_process_handle)'''%(exec_path, getpid(), crhandle, cwhandle)

to_child = open( open_osfhandle( pwhandle, 0 ), 'wb', closefd=True )

cmd = '%s -I -S -c "%s" --multiprocessing-fork'%(executable, subprocess_code)
# start process
try:
    subprocess_handle, thread_handle, pid, tid = CreateProcess( executable, cmd, None, None, False, 0, None, None, None)
    CloseHandle(thread_handle)
except:
    CloseHandle(crhandle)
    raise

# send information to child
to_child.write(b'success!') # don't send ...

You must...

I don't, actually.

ddanbe 2,720

What have you done already?

You must then use a loop (for or while) to find the sum of all the numbers in the geometric sequence of 3 less than N.
The geometric sequence of 3 looks like this
1, 3, 9, 27, 81, 243, ... where you start with the number one and multiply the previous number by 3 to get the next number.
add the sequences together to get answer

I should probably note, the interpreter I'm using is a portable Anaconda 2.3.0 (need to upgrade to 2.5.0) interpreter specifically tailored to work with my API, and as such is designed to be as minimal as possible (only 10.8 MiB in app/ folder size).
it does NOT include the standard library (so no os module), nor does it include builtin C modules I won't be using (such as _tkinter).

all it has is 2 standard modules being io (modified to remove abc and _weakrefset), and codecs (includes the encodings/ module (folder))

the app/ folder root looks like this:
https://cdn.discordapp.com/attachments/161204326218465280/491923337434628096/unknown.png

and to show the only modules I have to work with:
https://cdn.discordapp.com/attachments/161204326218465280/492146476760301578/unknown.png

everything here is either built in C if not built into python34
it's enough to start a subprocess though and import what I need, as it works with the above code, and I've already reimplemented most of what's actually needed from the standard library in my API.

so yeah, know that I can't use os or any of that stuff. ;)
(most of the standard library is CPU-heavy anyways, which is why it was removed from my API's environment)
^ regardless, it shouldn't be needed as my API implements better automated detection for it's libs and plugins.

EDIT: it's official, image support is broken.