问:
document status changes in time in firebase javascript app
I'm creating a game where users are fighting each other on own devices in firestore document which is an instance of a game. The problem is that I want to create an unbreakable solution to change game status in time. So the source of truth should be firestore.

There are few statuses indicated by the game creator (created, counting), but after a few seconds of counting, I need to set a few statuses without user action. So next statuses are inProgress, scoreSummary.

```<code>const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()

exports.gameStateChanger = functions.firestore
    .document('rooms/{roomId}')
    .onUpdate((change, context) => {

      const roomId = context.params.roomId
      const roomRef = admin.firestore().collection('rooms').doc(roomId)

      const newValue = change.after.data()
      console.log(newValue)

      const oldValue = change.before.data()

      if(newValue.game.status==='counting') {
          setTimeout(() => {
              console.log('timeout 5s')
              roomRef.update({state: 'inProgress'})
              setTimeout(() => {
                  roomRef.update({state: 'scoreSummary'})
              }, 60000)
          }, 5000)
      }
    })
</code>```

I tried to write a firebase function, but setTimeout in cloud function is not the best solution due to time execution limitations. I also see in the aprils update there is a firebase schedule but I want to change statuses a few seconds after the specific user clicks not every few seconds, and the schedule is also expensive.

Maybe someone has some experience with similar solutions and would tell which way should go?

答:
Your code seems to show that you are looking for delays of about 65 seconds. If that is the case, you can increase the timeout for your function to something slightly longer than 65 seconds: https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation

Another option would be to let your client code poll every few seconds by calling a server-side function. The server-side function would check the database to see if the right amount of time has passed. If it has, the function could update the 'state' field in the database, or simply return the new state to the client.

Body:
I'm creating a game where users are fighting each other on own devices in firestore document which is an instance of a game. The problem is that I want to create an unbreakable solution to change game status in time. So the source of truth should be firestore.

There are few statuses indicated by the game creator (created, counting), but after a few seconds of counting, I need to set a few statuses without user action. So next statuses are inProgress, scoreSummary.

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()

exports.gameStateChanger = functions.firestore
    .document('rooms/{roomId}')
    .onUpdate((change, context) => {

      const roomId = context.params.roomId
      const roomRef = admin.firestore().collection('rooms').doc(roomId)

      const newValue = change.after.data()
      console.log(newValue)

      const oldValue = change.before.data()

      if(newValue.game.status==='counting') {
          setTimeout(() => {
              console.log('timeout 5s')
              roomRef.update({state: 'inProgress'})
              setTimeout(() => {
                  roomRef.update({state: 'scoreSummary'})
              }, 60000)
          }, 5000)
      }
    })

I tried to write a firebase function, but setTimeout in cloud function is not the best solution due to time execution limitations. I also see in the aprils update there is a firebase schedule but I want to change statuses a few seconds after the specific user clicks not every few seconds, and the schedule is also expensive.

Maybe someone has some experience with similar solutions and would tell which way should go?


来源: stackexchange

问:
Problems making an awk between range of columns
I have a file called sds

```<code>$head sds
2557 386 fs://name/user/hive/ware/doc1/do_fact/date=20190313/fact=6
2593 393 fs://name/user/hive/ware/toc1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/user/hive/ware/dac2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/user/hive/ware/tec2/do_des/mes=25
340  332 fs://name/user/hive/ware/dc1/venta/year=2018/month=12
</code>```

I want delete /user/hive/ware and replace $7 ~ /_1$ for 1and other $7 for 2 using awk.

The code that I used was:

```<code>awk -F"/" '{ if ($7 ~ /_1$/)
print $1"//"$3"/1/"$7-$NF
else
print $1"//"$3"/2/"$7-$NF}' sds
</code>```

but the result is bad.

I would like and output like:

```<code>2557 386 fs://name/1/do_fact/date=20190313/fact=6
2593 393 fs://name/1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/2/do_des/mes=25
340  332 fs://name/1/venta/year=2018/month=12
</code>```

答:
```<code>$ awk 'BEGIN{FS=OFS="/"} {sub("/user/hive/ware",""); $4=($4~/1$/ ? 1 : 2)} 1' file
2557 386 fs://name/1/do_fact/date=20190313/fact=6
2593 393 fs://name/1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/2/do_des/mes=25
340  332 fs://name/1/venta/year=2018/month=12
</code>```

or if you don't REALLY want to remove the string /user/hive/share and instead want to remove the 4th through 6th fields no matter their value:

```<code>$ awk 'BEGIN{FS=OFS="/"} {$4=$5=$6="\n"; sub(/(\/\n){3}/,""); $4=($4~/1$/ ? 1 : 2)} 1' file
2557 386 fs://name/1/do_fact/date=20190313/fact=6
2593 393 fs://name/1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/2/do_des/mes=25
340  332 fs://name/1/venta/year=2018/month=12
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have a file called sds

$head sds
2557 386 fs://name/user/hive/ware/doc1/do_fact/date=20190313/fact=6
2593 393 fs://name/user/hive/ware/toc1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/user/hive/ware/dac2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/user/hive/ware/tec2/do_des/mes=25
340  332 fs://name/user/hive/ware/dc1/venta/year=2018/month=12

I want delete /user/hive/ware and replace $7 ~ /_1$ for 1and other $7 for 2 using awk.

The code that I used was:

awk -F"/" '{ if ($7 ~ /_1$/)
print $1"//"$3"/1/"$7-$NF
else
print $1"//"$3"/2/"$7-$NF}' sds

but the result is bad.

I would like and output like:

2557 386 fs://name/1/do_fact/date=20190313/fact=6
2593 393 fs://name/1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/2/do_des/mes=25
340  332 fs://name/1/venta/year=2018/month=12

Body:
$ awk 'BEGIN{FS=OFS="/"} {sub("/user/hive/ware",""); $4=($4~/1$/ ? 1 : 2)} 1' file
2557 386 fs://name/1/do_fact/date=20190313/fact=6
2593 393 fs://name/1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/2/do_des/mes=25
340  332 fs://name/1/venta/year=2018/month=12

or if you don't REALLY want to remove the string /user/hive/share and instead want to remove the 4th through 6th fields no matter their value:

$ awk 'BEGIN{FS=OFS="/"} {$4=$5=$6="\n"; sub(/(\/\n){3}/,""); $4=($4~/1$/ ? 1 : 2)} 1' file
2557 386 fs://name/1/do_fact/date=20190313/fact=6
2593 393 fs://name/1/do_gas/idi_centr=6372/mes=20
2594 343 fs://name/2/do_gas2/idi_centr=6354/mes=21
349  307 fs://name/2/do_des/mes=25
340  332 fs://name/1/venta/year=2018/month=12


问:
BSP dungeon generation is not generating corridors
I'm making a 2D RPG for my graduation project, and I'm at the point where I want to generate my dungeons randomly.

I'm following this tutorial http://www.rombdn.com/blog/2018/01/12/random-dungeon-bsp-unity/ to make a randomly generated dungeon.

After tons of research, I've found that the BSP algorithm generates the dungeon design I desire for my game. I've followed the tutorial provided in the link, step by step. I provided my script for you to see it:

```<code>public class BoardManager : MonoBehaviour {
    public GameObject floorTile;
    public GameObject corridorTile;

public int boardRows, boardColumns;
public int minRoomSize, maxRoomSize;

private GameObject[,] boardPositionsFloor;

public class SubDungeon {

    public SubDungeon left, right;
    public Rect rect;
    public Rect room = new Rect(-1,-1, 0, 0); // i.e null
    public int debugId;

    private static int debugCounter = 0;

    public List<Rect> corridors = new List<Rect>();

    public SubDungeon(Rect mrect) {
        rect = mrect;
        debugId = debugCounter;
        debugCounter++;
    }

    public bool IAmLeaf() {
        return left == null && right == null;
    }

    public bool Split(int minRoomSize, int maxRoomSize) {
        if (!IAmLeaf()) {
            return false;
        }

        // choose a vertical or horizontal split depending on the proportions
        // i.e. if too wide split vertically, or too long horizontally,
        // or if nearly square choose vertical or horizontal at random
        bool splitH;
        if (rect.width / rect.height >= 1.25) {
            splitH = false;
        } else if (rect.height / rect.width >= 1.25) {
            splitH = true;
        } else {
            splitH = Random.Range (0.0f, 1.0f) > 0.5;
        }

        if (Mathf.Min(rect.height, rect.width) / 2 < minRoomSize) {
            Debug.Log ("Sub-dungeon " + debugId + " will be a leaf");
            return false;
        }

        if (splitH) {
            // split so that the resulting sub-dungeons widths are not too small
            // (since we are splitting horizontally)
            int split = Random.Range (minRoomSize, (int)(rect.width - minRoomSize));

            left = new SubDungeon (new Rect (rect.x, rect.y, rect.width, split));
            right = new SubDungeon (
                new Rect (rect.x, rect.y + split, rect.width, rect.height - split));
        }
        else {
            int split = Random.Range (minRoomSize, (int)(rect.height - minRoomSize));

            left = new SubDungeon (new Rect (rect.x, rect.y, split, rect.height));
            right = new SubDungeon (
                new Rect (rect.x + split, rect.y, rect.width - split, rect.height));
        }

        return true;
    }

    public void CreateRoom() {
        if (left != null) {
            left.CreateRoom ();
        }
        if (right != null) {
            right.CreateRoom ();
        }
        if (IAmLeaf()) {
            int roomWidth = (int)Random.Range (rect.width / 2, rect.width - 2);
            int roomHeight = (int)Random.Range (rect.height / 2, rect.height - 2);
            int roomX = (int)Random.Range (1, rect.width - roomWidth - 1);
            int roomY = (int)Random.Range (1, rect.height - roomHeight - 1);

            // room position will be absolute in the board, not relative to the sub-dungeon
            room = new Rect (rect.x + roomX, rect.y + roomY, roomWidth, roomHeight);
            Debug.Log ("Created room " + room + " in sub-dungeon " + debugId + " " + rect);
        }
    }

    public Rect GetRoom() {
        if (IAmLeaf()) {
            return room;
        }
        if (left != null) {
            Rect lroom = left.GetRoom ();
            if (lroom.x != -1) {
                return lroom;
            }
        }
        if (right != null) {
            Rect rroom = right.GetRoom ();
            if (rroom.x != -1) {
                return rroom;
            }
        }

        // workaround non nullable structs
        return new Rect (-1, -1, 0, 0);
    }

    public void CreateCorridorBetween(SubDungeon left, SubDungeon right) {
        Rect lroom = left.GetRoom ();
        Rect rroom = right.GetRoom ();

        Debug.Log("Creating corridor(s) between " + left.debugId + "(" + lroom + ") and " + right.debugId + " (" + rroom + ")");
        Debug.Log ("hello");
        // attach the corridor to a random point in each room
        Vector2 lpoint = new Vector2 ((int)Random.Range (lroom.x + 1, lroom.xMax - 1), (int)Random.Range (lroom.y + 1, lroom.yMax - 1));
        Vector2 rpoint = new Vector2 ((int)Random.Range (rroom.x + 1, rroom.xMax - 1), (int)Random.Range (rroom.y + 1, rroom.yMax - 1));

        // always be sure that left point is on the left to simplify the code
        if (lpoint.x > rpoint.x) {
            Vector2 temp = lpoint;
            lpoint = rpoint;
            rpoint = temp;
        }

        int w = (int)(lpoint.x - rpoint.x);
        int h = (int)(lpoint.y - rpoint.y);

        Debug.Log ("lpoint: " + lpoint + ", rpoint: " + rpoint + ", w: " + w + ", h: " + h);

        // if the points are not aligned horizontally
        if (w != 0) {
            // choose at random to go horizontal then vertical or the opposite
            if (Random.Range (0, 1) > 2) {
                // add a corridor to the right
                corridors.Add (new Rect (lpoint.x, lpoint.y, Mathf.Abs (w) + 1, 1));

                // if left point is below right point go up
                // otherwise go down
                if (h < 0) {
                    corridors.Add (new Rect (rpoint.x, lpoint.y, 1, Mathf.Abs (h)));
                } else {
                    corridors.Add (new Rect (rpoint.x, lpoint.y, 1, -Mathf.Abs (h)));
                }
            } else {
                // go up or down
                if (h < 0) {
                    corridors.Add (new Rect (lpoint.x, lpoint.y, 1, Mathf.Abs (h)));
                } else {
                    corridors.Add (new Rect (lpoint.x, rpoint.y, 1, Mathf.Abs (h)));
                }

                // then go right
                corridors.Add (new Rect (lpoint.x, rpoint.y, Mathf.Abs (w) + 1, 1));
            }
        } else {
            // if the points are aligned horizontally
            // go up or down depending on the positions
            if (h < 0) {
                corridors.Add (new Rect ((int)lpoint.x, (int)lpoint.y, 1, Mathf.Abs (h)));
            } else {
                corridors.Add (new Rect ((int)rpoint.x, (int)rpoint.y, 1, Mathf.Abs (h)));
            }
        }

        Debug.Log ("Corridors: ");
        foreach (Rect corridor in corridors) {
            Debug.Log ("corridor: " + corridor);
        }
    }
}

public void CreateBSP(SubDungeon subDungeon) {
    Debug.Log ("Splitting sub-dungeon " + subDungeon.debugId + ": " + subDungeon.rect);
    if (subDungeon.IAmLeaf()) {
        // if the sub-dungeon is too large
        if (subDungeon.rect.width > maxRoomSize
            || subDungeon.rect.height > maxRoomSize
            || Random.Range(0.0f,1.0f) > 0.25) {

            if (subDungeon.Split (minRoomSize, maxRoomSize)) {
                Debug.Log ("Splitted sub-dungeon " + subDungeon.debugId + " in "
                    + subDungeon.left.debugId + ": " + subDungeon.left.rect + ", "
                    + subDungeon.right.debugId + ": " + subDungeon.right.rect);

                CreateBSP(subDungeon.left);
                CreateBSP(subDungeon.right);
            }
        }
    }
}
public void DrawRooms(SubDungeon subDungeon) {
    if (subDungeon == null) {
        return;
    }
    if (subDungeon.IAmLeaf()) {
        for (int i = (int)subDungeon.room.x; i < subDungeon.room.xMax; i++) {
            for (int j = (int)subDungeon.room.y; j < subDungeon.room.yMax; j++) {
                GameObject instance = Instantiate (floorTile, new Vector3 (i, j, 0f), Quaternion.identity) as GameObject;
                instance.transform.SetParent (transform);
                boardPositionsFloor [i, j] = instance;
            }
        }
    } else {
        DrawRooms (subDungeon.left);
        DrawRooms (subDungeon.right);
    }
}

void DrawCorridors(SubDungeon subDungeon) {
    if (subDungeon == null) {
        return;
    }

    DrawCorridors (subDungeon.left);
    DrawCorridors (subDungeon.right);

    foreach (Rect corridor in subDungeon.corridors) {
        for (int i = (int)corridor.x; i < corridor.xMax; i++) {
            for (int j = (int)corridor.y; j < corridor.yMax; j++) {
                if (boardPositionsFloor [i, j] == null) {
                    GameObject instance = Instantiate (corridorTile, new Vector3 (i, j, 0f), Quaternion.identity) as GameObject;
                    instance.transform.SetParent (transform);
                    boardPositionsFloor [i, j] = instance;
                }
            }
        }
    }
}

void Start () {
    SubDungeon rootSubDungeon = new SubDungeon (new Rect (0, 0, boardRows, boardColumns));
    CreateBSP (rootSubDungeon);
    rootSubDungeon.CreateRoom ();

    boardPositionsFloor = new GameObject[boardRows, boardColumns];
    DrawCorridors (rootSubDungeon);
    DrawRooms (rootSubDungeon);
}
}
</code>```

The rooms are being created perfectly and there are no error messages. I've noticed that the CreateCorridorBetween isn't being called from anywhere, even on the author's blog; therefore, the corridors aren't being created. How do I call it in my code?

答:
Considering the parameters of CreateCorridorBetween are SubDungeon left and SubDungeon right you would want to call the method after you create the rooms. I assume you have read the blog that you linked to but as the writer states

Connect the rooms: create corridors between rooms

Isolated rooms are not very useful, so we’ll add corridors between them. To do so, we’ll connect each leaf to its sibling. Then going up one level in the tree, we’ll repeat the process to connect parents sub-dungeons, until finally we connect the two initial sub-dungeons (see the stackexchange answer for illustrations).

This implies that there is more to this then a basic "just call it" solution.

Hope this helps and good luck on your project!

Body:
I'm making a 2D RPG for my graduation project, and I'm at the point where I want to generate my dungeons randomly.

I'm following this tutorial http://www.rombdn.com/blog/2018/01/12/random-dungeon-bsp-unity/ to make a randomly generated dungeon.

After tons of research, I've found that the BSP algorithm generates the dungeon design I desire for my game. I've followed the tutorial provided in the link, step by step. I provided my script for you to see it:

public class BoardManager : MonoBehaviour {
    public GameObject floorTile;
    public GameObject corridorTile;

public int boardRows, boardColumns;
public int minRoomSize, maxRoomSize;

private GameObject[,] boardPositionsFloor;

public class SubDungeon {

    public SubDungeon left, right;
    public Rect rect;
    public Rect room = new Rect(-1,-1, 0, 0); // i.e null
    public int debugId;

    private static int debugCounter = 0;

    public List<Rect> corridors = new List<Rect>();

    public SubDungeon(Rect mrect) {
        rect = mrect;
        debugId = debugCounter;
        debugCounter++;
    }

    public bool IAmLeaf() {
        return left == null && right == null;
    }

    public bool Split(int minRoomSize, int maxRoomSize) {
        if (!IAmLeaf()) {
            return false;
        }

        // choose a vertical or horizontal split depending on the proportions
        // i.e. if too wide split vertically, or too long horizontally,
        // or if nearly square choose vertical or horizontal at random
        bool splitH;
        if (rect.width / rect.height >= 1.25) {
            splitH = false;
        } else if (rect.height / rect.width >= 1.25) {
            splitH = true;
        } else {
            splitH = Random.Range (0.0f, 1.0f) > 0.5;
        }

        if (Mathf.Min(rect.height, rect.width) / 2 < minRoomSize) {
            Debug.Log ("Sub-dungeon " + debugId + " will be a leaf");
            return false;
        }

        if (splitH) {
            // split so that the resulting sub-dungeons widths are not too small
            // (since we are splitting horizontally)
            int split = Random.Range (minRoomSize, (int)(rect.width - minRoomSize));

            left = new SubDungeon (new Rect (rect.x, rect.y, rect.width, split));
            right = new SubDungeon (
                new Rect (rect.x, rect.y + split, rect.width, rect.height - split));
        }
        else {
            int split = Random.Range (minRoomSize, (int)(rect.height - minRoomSize));

            left = new SubDungeon (new Rect (rect.x, rect.y, split, rect.height));
            right = new SubDungeon (
                new Rect (rect.x + split, rect.y, rect.width - split, rect.height));
        }

        return true;
    }

    public void CreateRoom() {
        if (left != null) {
            left.CreateRoom ();
        }
        if (right != null) {
            right.CreateRoom ();
        }
        if (IAmLeaf()) {
            int roomWidth = (int)Random.Range (rect.width / 2, rect.width - 2);
            int roomHeight = (int)Random.Range (rect.height / 2, rect.height - 2);
            int roomX = (int)Random.Range (1, rect.width - roomWidth - 1);
            int roomY = (int)Random.Range (1, rect.height - roomHeight - 1);

            // room position will be absolute in the board, not relative to the sub-dungeon
            room = new Rect (rect.x + roomX, rect.y + roomY, roomWidth, roomHeight);
            Debug.Log ("Created room " + room + " in sub-dungeon " + debugId + " " + rect);
        }
    }

    public Rect GetRoom() {
        if (IAmLeaf()) {
            return room;
        }
        if (left != null) {
            Rect lroom = left.GetRoom ();
            if (lroom.x != -1) {
                return lroom;
            }
        }
        if (right != null) {
            Rect rroom = right.GetRoom ();
            if (rroom.x != -1) {
                return rroom;
            }
        }

        // workaround non nullable structs
        return new Rect (-1, -1, 0, 0);
    }

    public void CreateCorridorBetween(SubDungeon left, SubDungeon right) {
        Rect lroom = left.GetRoom ();
        Rect rroom = right.GetRoom ();

        Debug.Log("Creating corridor(s) between " + left.debugId + "(" + lroom + ") and " + right.debugId + " (" + rroom + ")");
        Debug.Log ("hello");
        // attach the corridor to a random point in each room
        Vector2 lpoint = new Vector2 ((int)Random.Range (lroom.x + 1, lroom.xMax - 1), (int)Random.Range (lroom.y + 1, lroom.yMax - 1));
        Vector2 rpoint = new Vector2 ((int)Random.Range (rroom.x + 1, rroom.xMax - 1), (int)Random.Range (rroom.y + 1, rroom.yMax - 1));

        // always be sure that left point is on the left to simplify the code
        if (lpoint.x > rpoint.x) {
            Vector2 temp = lpoint;
            lpoint = rpoint;
            rpoint = temp;
        }

        int w = (int)(lpoint.x - rpoint.x);
        int h = (int)(lpoint.y - rpoint.y);

        Debug.Log ("lpoint: " + lpoint + ", rpoint: " + rpoint + ", w: " + w + ", h: " + h);

        // if the points are not aligned horizontally
        if (w != 0) {
            // choose at random to go horizontal then vertical or the opposite
            if (Random.Range (0, 1) > 2) {
                // add a corridor to the right
                corridors.Add (new Rect (lpoint.x, lpoint.y, Mathf.Abs (w) + 1, 1));

                // if left point is below right point go up
                // otherwise go down
                if (h < 0) {
                    corridors.Add (new Rect (rpoint.x, lpoint.y, 1, Mathf.Abs (h)));
                } else {
                    corridors.Add (new Rect (rpoint.x, lpoint.y, 1, -Mathf.Abs (h)));
                }
            } else {
                // go up or down
                if (h < 0) {
                    corridors.Add (new Rect (lpoint.x, lpoint.y, 1, Mathf.Abs (h)));
                } else {
                    corridors.Add (new Rect (lpoint.x, rpoint.y, 1, Mathf.Abs (h)));
                }

                // then go right
                corridors.Add (new Rect (lpoint.x, rpoint.y, Mathf.Abs (w) + 1, 1));
            }
        } else {
            // if the points are aligned horizontally
            // go up or down depending on the positions
            if (h < 0) {
                corridors.Add (new Rect ((int)lpoint.x, (int)lpoint.y, 1, Mathf.Abs (h)));
            } else {
                corridors.Add (new Rect ((int)rpoint.x, (int)rpoint.y, 1, Mathf.Abs (h)));
            }
        }

        Debug.Log ("Corridors: ");
        foreach (Rect corridor in corridors) {
            Debug.Log ("corridor: " + corridor);
        }
    }
}

public void CreateBSP(SubDungeon subDungeon) {
    Debug.Log ("Splitting sub-dungeon " + subDungeon.debugId + ": " + subDungeon.rect);
    if (subDungeon.IAmLeaf()) {
        // if the sub-dungeon is too large
        if (subDungeon.rect.width > maxRoomSize
            || subDungeon.rect.height > maxRoomSize
            || Random.Range(0.0f,1.0f) > 0.25) {

            if (subDungeon.Split (minRoomSize, maxRoomSize)) {
                Debug.Log ("Splitted sub-dungeon " + subDungeon.debugId + " in "
                    + subDungeon.left.debugId + ": " + subDungeon.left.rect + ", "
                    + subDungeon.right.debugId + ": " + subDungeon.right.rect);

                CreateBSP(subDungeon.left);
                CreateBSP(subDungeon.right);
            }
        }
    }
}
public void DrawRooms(SubDungeon subDungeon) {
    if (subDungeon == null) {
        return;
    }
    if (subDungeon.IAmLeaf()) {
        for (int i = (int)subDungeon.room.x; i < subDungeon.room.xMax; i++) {
            for (int j = (int)subDungeon.room.y; j < subDungeon.room.yMax; j++) {
                GameObject instance = Instantiate (floorTile, new Vector3 (i, j, 0f), Quaternion.identity) as GameObject;
                instance.transform.SetParent (transform);
                boardPositionsFloor [i, j] = instance;
            }
        }
    } else {
        DrawRooms (subDungeon.left);
        DrawRooms (subDungeon.right);
    }
}

void DrawCorridors(SubDungeon subDungeon) {
    if (subDungeon == null) {
        return;
    }

    DrawCorridors (subDungeon.left);
    DrawCorridors (subDungeon.right);

    foreach (Rect corridor in subDungeon.corridors) {
        for (int i = (int)corridor.x; i < corridor.xMax; i++) {
            for (int j = (int)corridor.y; j < corridor.yMax; j++) {
                if (boardPositionsFloor [i, j] == null) {
                    GameObject instance = Instantiate (corridorTile, new Vector3 (i, j, 0f), Quaternion.identity) as GameObject;
                    instance.transform.SetParent (transform);
                    boardPositionsFloor [i, j] = instance;
                }
            }
        }
    }
}

void Start () {
    SubDungeon rootSubDungeon = new SubDungeon (new Rect (0, 0, boardRows, boardColumns));
    CreateBSP (rootSubDungeon);
    rootSubDungeon.CreateRoom ();

    boardPositionsFloor = new GameObject[boardRows, boardColumns];
    DrawCorridors (rootSubDungeon);
    DrawRooms (rootSubDungeon);
}
}

The rooms are being created perfectly and there are no error messages. I've noticed that the CreateCorridorBetween isn't being called from anywhere, even on the author's blog; therefore, the corridors aren't being created. How do I call it in my code?


问:
django error: django.urls.exceptions.NoReverseMatch
I am creating a stock management system for my company. I don't know why the error: NoReverseMatch keeps coming for item_edit and item_delete views or urls. Because of these errors, I am not able to comple my CRUD views and modify or delete a exisiting item.

The Error: "django.urls.exceptions.NoReverseMatch: Reverse for 'stock_management.views.item_edit' not found. "

I have already tried going through my code a lot of time. I have also tried calling reverse() function for item_edit and item_delete from the shell and I also have also tried to manually enter the URLs in the browser but still the same error keeps showing.

My models:

class Item(models.Model):
    GOLD_PURITY_CHOICES = (
        ...
    )
    COLOUR_CHOICES = (
        ...
    )
    DIAMOND_PURITY_CHOICES = (
        ...
    )
    RATING_CHOICES = (
        ...
    )

    code = models.CharField(max_length=25, db_index=True, unique=True)
    gold_purity = models.CharField(
        max_length=3, choices=GOLD_PURITY_CHOICES, default='14K')
    labour = models.PositiveIntegerField()
    certification_no = models.CharField(max_length=35, null=True, blank=True)
    diamond_colour = models.CharField(
        max_length=4, choices=COLOUR_CHOICES, default='F')
    diamond_purity = models.CharField(
        max_length=10, choices=DIAMOND_PURITY_CHOICES, default='IF')
    rating = models.CharField(
        max_length=3, default='A', choices=RATING_CHOICES)
    gross = models.DecimalField(max_digits=6, decimal_places=3)
    image = models.ImageField(blank=True, null=True,
                              upload_to=user_directory_path)

    def __str__(self):
       ...

class Color(models.Model):
    item = models.ForeignKey(
        Item, on_delete=models.CASCADE, related_name='colors', help_text='Item to which the colors belong.')
    shade = models.DecimalField(...)
    price = models.IntegerField(...)

class Diamond(models.Model):
    item = models.ForeignKey(
        Item, on_delete=models.CASCADE, related_name='diamonds', help_text='Item to which the diamond belong.')
    weight = models.DecimalField(...)
    rate = models.IntegerField(...)

```

<p>urls.py:</p>

<pre class="lang-py prettyprint-override"><code>from django.urls import path, include
from .views import *
from django.conf import settings

app_name = 'stock_management'

urlpatterns = [
    path('', index, name='homepage'),
    path('stock/', stock_list, name='stock_list'),
    path('stock/add', stock_create_view, name='add_stock'),

    # Item:
    path('item/edit/<pk>', item_edit, name='item_edit'),
    path('item/delete/<pk>', ItemDeleteView.as_view(), name='item_delete'),
]

if settings.DEBUG:
    # test mode
    from django.conf.urls.static import static
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

</code>```

views.py:

def item_edit(request, pk=1):
    item = get_object_or_404(Item, pk=pk)
    ColorInlineFormSet = inlineformset_factory(
        Item, Color, fields=('shade', 'price'), extra=1)
    DiamondInlineFormSet = inlineformset_factory(
        Item, Diamond, fields=('weight', 'rate'), extra=1)

    if request.method == 'POST':
        color_inline_formset = ColorInlineFormSet(
            request.POST, instance=item)
        diamond_inline_formset = DiamondInlineFormSet(
            request.POST, instance=item)
        item_form = ItemForm(request.POST, instance=item)

       if item_form.is_valid() and color_inline_formset.is_valid() and diamond_inline_formset.is_valid():
            item = item_form.save()
            color_formset = color_inline_formset.save()
            diamond_formset = diamond_inline_formset.save()
            return redirect('stock_management:homepage')
        else:
            messages.error(request, item_form.errors)
            messages.error(request, color_inline_formset.errors)
            messages.error(request, diamond_inline_formset.errors)

    else:
        color_inline_formset = ColorInlineFormSet(
            instance=item)
        diamond_inline_formset = DiamondInlineFormSet(
            instance=item)
        item_form = ItemForm(instance=item)

    return render(request, 'forms/add_stock.html', {'title': 'Edit Item Form', 'item': item, 'item_form': item_form, 'item_colour_forms': color_inline_formset, 'item_diamond_forms': diamond_inline_formset, })

def item_delete(request, pk):
    item = get_object_or_404(Item, pk=pk)
    if request.method == 'POST':
        item.delete()
        return redirect('stock_list')
    return render(request, 'forms/utils/confirm_delete.html', {'title': item})
```

<p>templates:
list.html:</p>

```{% extends 'index.html' %}
{% block title %} Stock {% endblock %}
{% block pageheading %} Stock List {% endblock %}
{% block content %}
{% for stock in stocks %}

{{ stock.item.code }}

    {% if stock.item.image %}
      <image src = " {{ stock.item.image.url }} " ></image>
    {% endif %}

{% endfor %}
{% endblock %}

```

I want the error to be resolved so that I can complete my CRUD views and also can access my list.html again.

答:
You need to add your appname with used urls in your html files when using urls.
Also update

```<code>   <a href=" {% url 'stock_management:item_delete' pk=stock.item.id %}
</code>```

to

```<code> <a href=" {% url 'stock_management:item_delete' stock.item.id %}
</code>```

You code should looks like that:

```<code>{% extends 'index.html' %}
{% block title %} Stock {% endblock %}
{% block pageheading %} Stock List {% endblock %}
{% block content %}
{% for stock in stocks %}
<div class="card-body">

  <div class="item pb-1">
    <h5>{{ stock.item.code }}</h5>
    {% if stock.item.image %}
      <image src = " {{ stock.item.image.url }} " ></image>
    {% endif %}
  <a href=" {% url 'stock_management:item_delete' stock.item.id %} " class="btn btn-danger btn-circle">
    <i class="fas fa-trash"></i>
  </a>
  <a href="{% url 'stock_management:item_edit' stock.item.id %}" class="btn btn-info btn-circle">
    <i class="far fa-edit"></i>
  </a>
</div>
{% endfor %}
{% endblock %}
</code>```

Body:
I am creating a stock management system for my company. I don't know why the error: NoReverseMatch keeps coming for item_edit and item_delete views or urls. Because of these errors, I am not able to comple my CRUD views and modify or delete a exisiting item.

The Error: "django.urls.exceptions.NoReverseMatch: Reverse for 'stock_management.views.item_edit' not found. "

I have already tried going through my code a lot of time. I have also tried calling reverse() function for item_edit and item_delete from the shell and I also have also tried to manually enter the URLs in the browser but still the same error keeps showing.

My models:

class Item(models.Model):
    GOLD_PURITY_CHOICES = (
        ...
    )
    COLOUR_CHOICES = (
        ...
    )
    DIAMOND_PURITY_CHOICES = (
        ...
    )
    RATING_CHOICES = (
        ...
    )

    code = models.CharField(max_length=25, db_index=True, unique=True)
    gold_purity = models.CharField(
        max_length=3, choices=GOLD_PURITY_CHOICES, default='14K')
    labour = models.PositiveIntegerField()
    certification_no = models.CharField(max_length=35, null=True, blank=True)
    diamond_colour = models.CharField(
        max_length=4, choices=COLOUR_CHOICES, default='F')
    diamond_purity = models.CharField(
        max_length=10, choices=DIAMOND_PURITY_CHOICES, default='IF')
    rating = models.CharField(
        max_length=3, default='A', choices=RATING_CHOICES)
    gross = models.DecimalField(max_digits=6, decimal_places=3)
    image = models.ImageField(blank=True, null=True,
                              upload_to=user_directory_path)

    def __str__(self):
       ...

class Color(models.Model):
    item = models.ForeignKey(
        Item, on_delete=models.CASCADE, related_name='colors', help_text='Item to which the colors belong.')
    shade = models.DecimalField(...)
    price = models.IntegerField(...)

class Diamond(models.Model):
    item = models.ForeignKey(
        Item, on_delete=models.CASCADE, related_name='diamonds', help_text='Item to which the diamond belong.')
    weight = models.DecimalField(...)
    rate = models.IntegerField(...)

urls.py:

from django.urls import path, include
from .views import *
from django.conf import settings

app_name = 'stock_management'

urlpatterns = [
    path('', index, name='homepage'),
    path('stock/', stock_list, name='stock_list'),
    path('stock/add', stock_create_view, name='add_stock'),

    # Item:
    path('item/edit/<pk>', item_edit, name='item_edit'),
    path('item/delete/<pk>', ItemDeleteView.as_view(), name='item_delete'),
]

if settings.DEBUG:
    # test mode
    from django.conf.urls.static import static
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

views.py:

def item_edit(request, pk=1):
    item = get_object_or_404(Item, pk=pk)
    ColorInlineFormSet = inlineformset_factory(
        Item, Color, fields=('shade', 'price'), extra=1)
    DiamondInlineFormSet = inlineformset_factory(
        Item, Diamond, fields=('weight', 'rate'), extra=1)

    if request.method == 'POST':
        color_inline_formset = ColorInlineFormSet(
            request.POST, instance=item)
        diamond_inline_formset = DiamondInlineFormSet(
            request.POST, instance=item)
        item_form = ItemForm(request.POST, instance=item)

       if item_form.is_valid() and color_inline_formset.is_valid() and diamond_inline_formset.is_valid():
            item = item_form.save()
            color_formset = color_inline_formset.save()
            diamond_formset = diamond_inline_formset.save()
            return redirect('stock_management:homepage')
        else:
            messages.error(request, item_form.errors)
            messages.error(request, color_inline_formset.errors)
            messages.error(request, diamond_inline_formset.errors)

    else:
        color_inline_formset = ColorInlineFormSet(
            instance=item)
        diamond_inline_formset = DiamondInlineFormSet(
            instance=item)
        item_form = ItemForm(instance=item)

    return render(request, 'forms/add_stock.html', {'title': 'Edit Item Form', 'item': item, 'item_form': item_form, 'item_colour_forms': color_inline_formset, 'item_diamond_forms': diamond_inline_formset, })

def item_delete(request, pk):
    item = get_object_or_404(Item, pk=pk)
    if request.method == 'POST':
        item.delete()
        return redirect('stock_list')
    return render(request, 'forms/utils/confirm_delete.html', {'title': item})

templates:
list.html:

{% extends 'index.html' %}
{% block title %} Stock {% endblock %}
{% block pageheading %} Stock List {% endblock %}
{% block content %}
{% for stock in stocks %}

{{ stock.item.code }}

    {% if stock.item.image %}
      <image src = " {{ stock.item.image.url }} " ></image>
    {% endif %}

{% endfor %}
{% endblock %}

I want the error to be resolved so that I can complete my CRUD views and also can access my list.html again.

Body:
You need to add your appname with used urls in your html files when using urls.
Also update

   <a href=" {% url 'stock_management:item_delete' pk=stock.item.id %}

to

 <a href=" {% url 'stock_management:item_delete' stock.item.id %}

You code should looks like that:

{% extends 'index.html' %}
{% block title %} Stock {% endblock %}
{% block pageheading %} Stock List {% endblock %}
{% block content %}
{% for stock in stocks %}
<div class="card-body">

  <div class="item pb-1">
    <h5>{{ stock.item.code }}</h5>
    {% if stock.item.image %}
      <image src = " {{ stock.item.image.url }} " ></image>
    {% endif %}
  <a href=" {% url 'stock_management:item_delete' stock.item.id %} " class="btn btn-danger btn-circle">
    <i class="fas fa-trash"></i>
  </a>
  <a href="{% url 'stock_management:item_edit' stock.item.id %}" class="btn btn-info btn-circle">
    <i class="far fa-edit"></i>
  </a>
</div>
{% endfor %}
{% endblock %}


问:
Spark ML_pipelines: managing table reading
I am using Spark ML_pipelines to easily deploy operations that I have developed in Sparklyr in a production environment using SCALA. It is working pretty well, except for one part: it seems that when I read a table from Hive and then create a pipeline that applies operations to this table the pipeline will also save the table reading operation and thereby the name of the table. However I want the pipeline to be independent of this.

Here is a reproducible example:

Sparklyr part:

```<code>sc = spark2_context(memory = "4G")

iris <- copy_to(sc, iris, overwrite=TRUE)

spark_write_table(iris, "base.iris")
spark_write_table(iris, "base.iris2")
df1 <- tbl(sc, "base.iris")

df2 <- df1 %>%
  mutate(foo = 5)

pipeline <- ml_pipeline(sc) %>%
  ft_dplyr_transformer(df2) %>%
  ml_fit(df1)

ml_save(pipeline,
        paste0(save_pipeline_path, "test_pipeline_reading_from_table"),
        overwrite = TRUE)

df2 <- pipeline %>% ml_transform(df1)

dbSendQuery(sc, "drop table base.iris")
</code>```

SCALApart:

```<code>import org.apache.spark.ml.PipelineModel
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf

val df1 = spark.sql("select * from base.iris2")

val pipeline = PipelineModel.load(pipeline_path + "/test_pipeline_reading_from_table")
val df2 = pipeline.transform(df1)
</code>```

I get this error:

```<code>org.apache.spark.sql.AnalysisException: Table or view not found: `base`.`iris`; line 2 pos 5;
'Project ['Sepal_Length, 'Sepal_Width, 'Petal_Length, 'Petal_Width, 'Species, 5.0 AS foo#110]
+- 'UnresolvedRelation `base`.`iris`

  at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
  at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1.apply(CheckAnalysis.scala:82)
  at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1.apply(CheckAnalysis.scala:78)
  at org.apache.spark.sql.catalyst.trees.TreeNode.foreachUp(TreeNode.scala:127)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$foreachUp$1.apply(TreeNode.scala:126)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$foreachUp$1.apply(TreeNode.scala:126)
  at scala.collection.immutable.List.foreach(List.scala:381)
  at org.apache.spark.sql.catalyst.trees.TreeNode.foreachUp(TreeNode.scala:126)
  at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$class.checkAnalysis(CheckAnalysis.scala:78)
  at org.apache.spark.sql.catalyst.analysis.Analyzer.checkAnalysis(Analyzer.scala:91)
  at org.apache.spark.sql.execution.QueryExecution.assertAnalyzed(QueryExecution.scala:52)
  at org.apache.spark.sql.Dataset$.ofRows(Dataset.scala:67)
  at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:637)
  at org.apache.spark.ml.feature.SQLTransformer.transformSchema(SQLTransformer.scala:86)
  at org.apache.spark.ml.PipelineModel$$anonfun$transformSchema$5.apply(Pipeline.scala:310)
  at org.apache.spark.ml.PipelineModel$$anonfun$transformSchema$5.apply(Pipeline.scala:310)
  at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:57)
  at scala.collection.IndexedSeqOptimized$class.foldLeft(IndexedSeqOptimized.scala:66)
  at scala.collection.mutable.ArrayOps$ofRef.foldLeft(ArrayOps.scala:186)
  at org.apache.spark.ml.PipelineModel.transformSchema(Pipeline.scala:310)
  at org.apache.spark.ml.PipelineStage.transformSchema(Pipeline.scala:74)
  at org.apache.spark.ml.PipelineModel.transform(Pipeline.scala:304)
  ... 71 elided
</code>```

I can see 2 solutions:

It seems that persisting dataframe would be a solution, but then I would need to find a way not to overload my memory, hence my question on unpersisting

Passing the name of the table in Hive as a parameter of the pipeline, which I am trying to solve in this question

Now, all of this being said, I might be missing something as I am only a beginner...

EDIT: this is significantly different from this question as this concerns the specific problem of integrating a dataframe that was just read in a pipeline, as specified in the title.

EDIT: as for my project, persisting the tables after I read them is a viable solution. I don't know if there is any better solution.

答:
Then the pipeline would call my table "base.table", making it impossible to apply it to another table.

That's actually not true. ft_dplyr_transformer is a syntactic sugar for Spark's own SQLTransformer. Internally dplyr expression is converted to SQL query, and the name of the table is replaced with __THIS__ (Spark placeholder referring to the current table).

Let's say you have transformation like this one:

copy_to(sc, iris, overwrite=TRUE)

df <- tbl(sc, "iris") %>%
  mutate(foo = 5)

pipeline <- ml_pipeline(sc) %>%
  ft_dplyr_transformer(df) %>%
  ml_fit(tbl(sc, "iris"))

ml_stage(pipeline, "dplyr_transformer") %>% spark_jobj() %>% invoke("getStatement")
```

<pre class="lang-none prettyprint-override"><code>[1] "SELECT `Sepal_Length`, `Sepal_Width`, `Petal_Length`, `Petal_Width`, `Species`, 5.0 AS `foo`\nFROM `__THIS__`"
</code>```

That's however rather confusing way expressing things, and it makes more sense to use native SQL transformer directly:

```<code>pipeline <- ml_pipeline(sc) %>%
  ft_sql_transformer("SELECT *, 5 as `foo` FROM __THIS__") %>%
  ml_fit(df)
</code>```

Edit:

The problem you experience here looks like a bug. get_base_name function returns unquoted table name, so the value in your case will be

```<code>> get_base_name(x$ops)
<IDENT> default.iris
</code>```

and the pattern will be

```<code>> pattern
[1] "\\bdefault.iris\\b"
</code>```

However dbplyr::sql_render returns backquoted fully qualified name:

```<code>> dbplyr::sql_render(x)
<SQL> SELECT `Sepal_Length`, `Sepal_Width`, `Petal_Length`, `Petal_Width`, `Species`, 5.0 AS `foo`
FROM `default`.`iris`
</code>```

So the pattern doesn't match the name.

Body:
I am using Spark ML_pipelines to easily deploy operations that I have developed in Sparklyr in a production environment using SCALA. It is working pretty well, except for one part: it seems that when I read a table from Hive and then create a pipeline that applies operations to this table the pipeline will also save the table reading operation and thereby the name of the table. However I want the pipeline to be independent of this.

Here is a reproducible example:

Sparklyr part:

sc = spark2_context(memory = "4G")

iris <- copy_to(sc, iris, overwrite=TRUE)

spark_write_table(iris, "base.iris")
spark_write_table(iris, "base.iris2")
df1 <- tbl(sc, "base.iris")

df2 <- df1 %>%
  mutate(foo = 5)

pipeline <- ml_pipeline(sc) %>%
  ft_dplyr_transformer(df2) %>%
  ml_fit(df1)

ml_save(pipeline,
        paste0(save_pipeline_path, "test_pipeline_reading_from_table"),
        overwrite = TRUE)

df2 <- pipeline %>% ml_transform(df1)

dbSendQuery(sc, "drop table base.iris")

SCALApart:

import org.apache.spark.ml.PipelineModel
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf

val df1 = spark.sql("select * from base.iris2")

val pipeline = PipelineModel.load(pipeline_path + "/test_pipeline_reading_from_table")
val df2 = pipeline.transform(df1)

I get this error:

org.apache.spark.sql.AnalysisException: Table or view not found: `base`.`iris`; line 2 pos 5;
'Project ['Sepal_Length, 'Sepal_Width, 'Petal_Length, 'Petal_Width, 'Species, 5.0 AS foo#110]
+- 'UnresolvedRelation `base`.`iris`

  at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
  at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1.apply(CheckAnalysis.scala:82)
  at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1.apply(CheckAnalysis.scala:78)
  at org.apache.spark.sql.catalyst.trees.TreeNode.foreachUp(TreeNode.scala:127)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$foreachUp$1.apply(TreeNode.scala:126)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$foreachUp$1.apply(TreeNode.scala:126)
  at scala.collection.immutable.List.foreach(List.scala:381)
  at org.apache.spark.sql.catalyst.trees.TreeNode.foreachUp(TreeNode.scala:126)
  at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$class.checkAnalysis(CheckAnalysis.scala:78)
  at org.apache.spark.sql.catalyst.analysis.Analyzer.checkAnalysis(Analyzer.scala:91)
  at org.apache.spark.sql.execution.QueryExecution.assertAnalyzed(QueryExecution.scala:52)
  at org.apache.spark.sql.Dataset$.ofRows(Dataset.scala:67)
  at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:637)
  at org.apache.spark.ml.feature.SQLTransformer.transformSchema(SQLTransformer.scala:86)
  at org.apache.spark.ml.PipelineModel$$anonfun$transformSchema$5.apply(Pipeline.scala:310)
  at org.apache.spark.ml.PipelineModel$$anonfun$transformSchema$5.apply(Pipeline.scala:310)
  at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:57)
  at scala.collection.IndexedSeqOptimized$class.foldLeft(IndexedSeqOptimized.scala:66)
  at scala.collection.mutable.ArrayOps$ofRef.foldLeft(ArrayOps.scala:186)
  at org.apache.spark.ml.PipelineModel.transformSchema(Pipeline.scala:310)
  at org.apache.spark.ml.PipelineStage.transformSchema(Pipeline.scala:74)
  at org.apache.spark.ml.PipelineModel.transform(Pipeline.scala:304)
  ... 71 elided

I can see 2 solutions:

It seems that persisting dataframe would be a solution, but then I would need to find a way not to overload my memory, hence my question on unpersisting

Passing the name of the table in Hive as a parameter of the pipeline, which I am trying to solve in this question

Now, all of this being said, I might be missing something as I am only a beginner...

EDIT: this is significantly different from this question as this concerns the specific problem of integrating a dataframe that was just read in a pipeline, as specified in the title.

EDIT: as for my project, persisting the tables after I read them is a viable solution. I don't know if there is any better solution.

Body:
Then the pipeline would call my table "base.table", making it impossible to apply it to another table.

That's actually not true. ft_dplyr_transformer is a syntactic sugar for Spark's own SQLTransformer. Internally dplyr expression is converted to SQL query, and the name of the table is replaced with __THIS__ (Spark placeholder referring to the current table).

Let's say you have transformation like this one:

copy_to(sc, iris, overwrite=TRUE)

df <- tbl(sc, "iris") %>%
  mutate(foo = 5)

pipeline <- ml_pipeline(sc) %>%
  ft_dplyr_transformer(df) %>%
  ml_fit(tbl(sc, "iris"))

ml_stage(pipeline, "dplyr_transformer") %>% spark_jobj() %>% invoke("getStatement")

[1] "SELECT `Sepal_Length`, `Sepal_Width`, `Petal_Length`, `Petal_Width`, `Species`, 5.0 AS `foo`\nFROM `__THIS__`"

That's however rather confusing way expressing things, and it makes more sense to use native SQL transformer directly:

pipeline <- ml_pipeline(sc) %>%
  ft_sql_transformer("SELECT *, 5 as `foo` FROM __THIS__") %>%
  ml_fit(df)

Edit:

The problem you experience here looks like a bug. get_base_name function returns unquoted table name, so the value in your case will be

> get_base_name(x$ops)
<IDENT> default.iris

and the pattern will be

> pattern
[1] "\\bdefault.iris\\b"

However dbplyr::sql_render returns backquoted fully qualified name:

> dbplyr::sql_render(x)
<SQL> SELECT `Sepal_Length`, `Sepal_Width`, `Petal_Length`, `Petal_Width`, `Species`, 5.0 AS `foo`
FROM `default`.`iris`

So the pattern doesn't match the name.


问:
How I create a chatbot which reads and writes to a SQL Server database?
I am new to MS bot framework and working on my project. I wanted to create a chatbot that can read from the database (I am using SQL Server for creating database) and also takes data from the users and saves it in the database. I have searched a lot but I was not able to find any tutorial regarding this. Can anyone help me and kindly show it with code on how to do this ? Thanks.</p>

答:
The approach I use in my project for Bot and DB:

Build a Rest APIs for request information to the DB. For example you can build a project in ASP.NET and use Entity Framework. Then build your repository... and finally build your controller's endpoints (Example: CRUDs operation).

Then use your bot's logic for make request to the Rest APIs endpoints. For examble use the HttpClient

Retrieve data from user:

Here you have a lot of options. Depending on what you need you can retrieve data using:

regular expression,

using Natural Language Processing (NLP) like LUIS.

Prompt user for data.See here

And more...

My best advice is to read the Microsoft Botframework Documentation to learn how bot work. For Web APIs, look for tutorials


问:
List of control sequences in \033[ (for urxvt keysym)
I am trying to remap cursor moves and such in my terminal, using Urxvt "keysym" resource. For example, this shortcut (in my  .Xresources) does previous-word :

*keysym.Control-h: \033[1;5D

I didn't find a complete (working) list of sequences like this 1;5D. I know a few others (Home, End...) but need some others (kill next word, kill last word, cancel last action...). The quest for this list is not new, but the answers aren't what they seem to be (not working, maybe not the \033[ type ?). I spent hours on Google, Wikipedia, manual pages (urxvt, readline, ascii, xterm, bash...), it looks like some kind of cruel initiation.

Where can I find this reference ?

答:
Here's at least a list of the function keys and some Ctrl combinations: https://jscape.kayako.com/knowledgebase/article/View/5/7/sending-ctrla-ctrlh-f1-f12-and-other-function-keys-to-telnet-server

Here's a more complete list in one of the suckless tool's source code: http://git.suckless.org/sites/commit/db1cf4df877e9083fadd67e6f16f1a5552effe99.html

And here's an actual explanation of how some of this works, especially modifier keys http://www.leonerd.org.uk/hacks/fixterms/


问:
How to add second viewcontroller textfield value to firstviewcontroller label in thirdviewcontroller button using delegate in swift
I have three viewcontrollers. here i want to add second viewcontroller textfield value to first viewcontroller tableview label from third viewcontroller done button.
I am able to add second viewcontroller textfield to first viewcontroller label using delegate while popviewcontroller, but i am unable to add from third viewcontroller button.
please help me in the code.

here is my code:

this is my firstVC code:

```<code>import UIKit
class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, EmployeeDelegateProtocol {

var namesArray = [String]()
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}
    func sendDataToEmployeeViewController(myData: String) {
    self.namesArray.append(myData)
    tableView.reloadData()
    print(namesArray)
    print("in delegate method \(namesArray)")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return namesArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
    cell.empName.text = namesArray[indexPath.row]
    return cell
}

@IBAction func addButtonClicked(_ sender: Any) {
    let empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
    empProfileVC.delegate = self
    self.navigationController?.pushViewController(empProfileVC, animated: true)
}
}
</code>```

this is my secondVC code:

while using pop it is working

```<code>import UIKit

protocol EmployeeDelegateProtocol {
func sendDataToEmployeeViewController(myData: String)
}

class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate,UIImagePickerControllerDelegate {

var delegate: EmployeeDelegateProtocol?

@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
}
@objc func saveButtonClicked(){
    print("button clicked")
    self.delegate?.sendDataToEmployeeViewController(myData: firstNameTextField.text!)
    //self.navigationController?.popViewController(animated: true)
}
}
</code>```

but i want to send the value from thirdVc done button:

```<code>import UIKit

class EmployeeProfilecreatedPopUpViewController: UIViewController {
var cellProfile: EmployeeProfileViewController?
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

@IBAction func doneButtonTapped(_ sender: Any) {

    print("done tapped")
    let empVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeViewController") as! EmployeeViewController        self.navigationController?.pushViewController(empVC, animated: true)
}
}
</code>```

please suggest me how to send from thirdvc done button.

答:
FirstViewController

```<code>import UIKit

class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, EmployeeDelegateProtocol {

    //MARK:- ----- Outlets and variables ------
    @IBOutlet weak var tableView: UITableView!
    var namesArray = [String]()

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    //MARK:- ----- TableView Datasource ------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return namesArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
        cell.empName.text = namesArray[indexPath.row]
        return cell
    }

    //MARK:- ----- Button Actions ------
    @IBAction func addButtonClicked(_ sender: Any) {

        let empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
        empProfileVC.delegate = self
        self.navigationController?.pushViewController(empProfileVC, animated: true)
    }

    //MARK:- ----- Employee delegate ------
    func popController(_ controller: UIViewController, withData: String) {

        self.namesArray.append(myData)
        tableView.reloadData()
        print(namesArray)
        print("in delegate method \(namesArray)")

        controller.navigationController?.popViewController(animated: false)
    }
}
</code>```

SecondViewController

```<code>import UIKit

protocol EmployeeDelegateProtocol {
    func popController(_ controller: UIViewController, withData: String)
}

 class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate, EmployeeProfileDelegate {

    //MARK:- ----- Outlets and variables ------
    var delegate: EmployeeDelegateProtocol?
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var tableView: UITableView!

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //MARK:- ----- Button Actions ------
    @objc func saveButtonClicked() {
        let popUpVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfilecreatedPopUpViewController") as! EmployeeProfilecreatedPopUpViewController
        popUpVC.delegate = self
        self.navigationController?.pushViewController(popUpVC, animated: true)
        // OR
        self.present(popUpVC, animated: true) { }

        /*print("button clicked")
        self.delegate?.sendDataToEmployeeViewController(myData: firstNameTextField.text!)*/
        //self.navigationController?.popViewController(animated: true)
    }

    //MARK:- ----- Employee profile delegate ------
    func popController(_ controller: UIViewController) {

        self.delegate?.popController(self, withData: firstNameTextField.text!)
        controller.navigationController?.popViewController(animated: false)

        // OR

        controller.dismiss(animated: true) {
            self.delegate?.popController(self, withData: self.firstNameTextField.text!)
        }
    }
}
</code>```

ThirdViewController

```<code>import UIKit

protocol EmployeeProfileDelegate {
    func popController(_ controller: UIViewController)
}

class EmployeeProfilecreatedPopUpViewController: UIViewController {

    //MARK:- ----- Outlets and variables ------
    var delegate: EmployeeProfileDelegate?

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    //MARK:- ----- Button Actions ------
    @IBAction func doneButtonTapped(_ sender: Any) {

        self.delegate?.popController(self)
        /*print("done tapped")
        let empVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeViewController") as! EmployeeViewController
        self.navigationController?.pushViewController(empVC, animated: true)*/
    }
}
</code>```

If you are using segue for SecondViewController to ThirdCiewControler, then refer this to unwind segue : Transition to UITabBarController

And after unwind segue from ThirdViewController to SecondViewController, call delegate method to pass data from SecondViewController to FirstViewController.

Body:
I have three viewcontrollers. here i want to add second viewcontroller textfield value to first viewcontroller tableview label from third viewcontroller done button.
I am able to add second viewcontroller textfield to first viewcontroller label using delegate while popviewcontroller, but i am unable to add from third viewcontroller button.
please help me in the code.

here is my code:

this is my firstVC code:

import UIKit
class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, EmployeeDelegateProtocol {

var namesArray = [String]()
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}
    func sendDataToEmployeeViewController(myData: String) {
    self.namesArray.append(myData)
    tableView.reloadData()
    print(namesArray)
    print("in delegate method \(namesArray)")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return namesArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
    cell.empName.text = namesArray[indexPath.row]
    return cell
}

@IBAction func addButtonClicked(_ sender: Any) {
    let empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
    empProfileVC.delegate = self
    self.navigationController?.pushViewController(empProfileVC, animated: true)
}
}

this is my secondVC code:

while using pop it is working

import UIKit

protocol EmployeeDelegateProtocol {
func sendDataToEmployeeViewController(myData: String)
}

class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate,UIImagePickerControllerDelegate {

var delegate: EmployeeDelegateProtocol?

@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
}
@objc func saveButtonClicked(){
    print("button clicked")
    self.delegate?.sendDataToEmployeeViewController(myData: firstNameTextField.text!)
    //self.navigationController?.popViewController(animated: true)
}
}

but i want to send the value from thirdVc done button:

import UIKit

class EmployeeProfilecreatedPopUpViewController: UIViewController {
var cellProfile: EmployeeProfileViewController?
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

@IBAction func doneButtonTapped(_ sender: Any) {

    print("done tapped")
    let empVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeViewController") as! EmployeeViewController        self.navigationController?.pushViewController(empVC, animated: true)
}
}

please suggest me how to send from thirdvc done button.

Body:
FirstViewController

import UIKit

class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, EmployeeDelegateProtocol {

    //MARK:- ----- Outlets and variables ------
    @IBOutlet weak var tableView: UITableView!
    var namesArray = [String]()

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    //MARK:- ----- TableView Datasource ------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return namesArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
        cell.empName.text = namesArray[indexPath.row]
        return cell
    }

    //MARK:- ----- Button Actions ------
    @IBAction func addButtonClicked(_ sender: Any) {

        let empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
        empProfileVC.delegate = self
        self.navigationController?.pushViewController(empProfileVC, animated: true)
    }

    //MARK:- ----- Employee delegate ------
    func popController(_ controller: UIViewController, withData: String) {

        self.namesArray.append(myData)
        tableView.reloadData()
        print(namesArray)
        print("in delegate method \(namesArray)")

        controller.navigationController?.popViewController(animated: false)
    }
}

SecondViewController

import UIKit

protocol EmployeeDelegateProtocol {
    func popController(_ controller: UIViewController, withData: String)
}

 class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate, EmployeeProfileDelegate {

    //MARK:- ----- Outlets and variables ------
    var delegate: EmployeeDelegateProtocol?
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var tableView: UITableView!

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //MARK:- ----- Button Actions ------
    @objc func saveButtonClicked() {
        let popUpVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfilecreatedPopUpViewController") as! EmployeeProfilecreatedPopUpViewController
        popUpVC.delegate = self
        self.navigationController?.pushViewController(popUpVC, animated: true)
        // OR
        self.present(popUpVC, animated: true) { }

        /*print("button clicked")
        self.delegate?.sendDataToEmployeeViewController(myData: firstNameTextField.text!)*/
        //self.navigationController?.popViewController(animated: true)
    }

    //MARK:- ----- Employee profile delegate ------
    func popController(_ controller: UIViewController) {

        self.delegate?.popController(self, withData: firstNameTextField.text!)
        controller.navigationController?.popViewController(animated: false)

        // OR

        controller.dismiss(animated: true) {
            self.delegate?.popController(self, withData: self.firstNameTextField.text!)
        }
    }
}

ThirdViewController

import UIKit

protocol EmployeeProfileDelegate {
    func popController(_ controller: UIViewController)
}

class EmployeeProfilecreatedPopUpViewController: UIViewController {

    //MARK:- ----- Outlets and variables ------
    var delegate: EmployeeProfileDelegate?

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    //MARK:- ----- Button Actions ------
    @IBAction func doneButtonTapped(_ sender: Any) {

        self.delegate?.popController(self)
        /*print("done tapped")
        let empVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeViewController") as! EmployeeViewController
        self.navigationController?.pushViewController(empVC, animated: true)*/
    }
}

If you are using segue for SecondViewController to ThirdCiewControler, then refer this to unwind segue : Transition to UITabBarController

And after unwind segue from ThirdViewController to SecondViewController, call delegate method to pass data from SecondViewController to FirstViewController.


问:
xslt showing a group of n xml nodes one at a time
update:

This question has some little xsl code in it !

I'm struggling with this problem for weeks (even months). I'm rather beginner through this xml/xslt area and I only need at least one working pagination technique for my xml data. I just can't seem to find just ONE working solution about this issue!!

I already state that, in my view, pagination is one of the most useful xml/xslt web topics and is one of the less discussed topics over the whole web. Only portions of it and most of them non-functional.
 Anyway, it's all about this stkovrflw question:

xslt xml table records pagination

where one could see my whole problem.
 It has been answered by michael.hor257k which I thank him very much.
 Point is it's only halfway working (as far as my requirement concerned)

I only need to display 5 nodes at a time, based on some parameter passed in which is the page number. I'll name it as

param name = "pagnmbr"

And, at first if this $pagnmbr is 1, that xslt will display:

01  02  03  04  05

Else, if it's 2 ($pagnmbr = 2), it will display:

06  07  08  09  10

And if same parameter become 3, the reading will be:

11  12  13  14  15

And so on. See, this kind of functionality I'm in very need for doing.

update:
I just did the following xslt code:

```<code><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="crrtPag" select="2"/>     <!-- current page -->
 <xsl:variable name="rcrdsppag" select="5"/> <want 5 displaying records per page -->

 <xsl:template name="pag" match="/group/item">
 <html>
  <body>
   <xsl:if test="position()-$crrtPag = ($rcrdsppag - 1)*($crrtPag - 1)">
    <xsl:call-template name="display_pag">
    <xsl:with-param name="crrtPag" select="item[position() mod $rcrdsppag = 1]"/>
    </xsl:call-template>
   </xsl:if>
   </body>
   </html>
  </xsl:template>

  <xsl:template name="display_pag" match="item"> <!-- item -->
   <xsl:param name="crrtPag"/>
     <xsl:for-each select="//item">
       <xsl:if test="position()<=5">
         <xsl:value-of select="."/>
       </xsl:if>
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
</code>```

But, as one could notice, it's kept showing only:

01 02 03 04 05

even if I put $pagnmbr = 2, or $pagnmbr = 3 .. pop up only the first 5 group items. It never shows up next node-set of

06  07  08 09  10

and so on ..

Please you guys help me with it !!!
 Thank you very much in advance.

答:
Given:

XML

```<code><group>
    <item> 01 </item>
    <item> 02 </item>
    <item> 03 </item>
    <item> 04 </item>
    <item> 05 </item>
    <item> 06 </item>
    <item> 07 </item>
    <item> 08 </item>
    <item> 09 </item>
    <item> 10 </item>
    <item> 11 </item>
    <item> 12 </item>
    <item> 13 </item>
    <item> 14 </item>
    <item> 15 </item>
    <item> 16 </item>
    <item> 17 </item>
    <item> 18 </item>
    <item> 19 </item>
    <item> 20 </item>
    <item> 21 </item>
    <item> 22 </item>
    <item> 23 </item>
    <item> 24 </item>
    <item> 25 </item>
    <item> 26 </item>
</group>
</code>```

the following stylesheet:

XSLT 1.0

```<code><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="pageSize" select="5" />
<xsl:param name="pageNumber" select="3" />

<xsl:template match="/group">
    <html>
        <body>
            <div id="page{$pageNumber}">
                <ul>
                    <xsl:variable name="start" select="$pageSize * ($pageNumber - 1) + 1" />
                    <xsl:for-each select="item[position() >= $start and position() < $start + $pageSize]">
                        <li>
                            <xsl:value-of select="."/>
                        </li>
                    </xsl:for-each>
                </ul>
            </div>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>
</code>```

will return:

Result

```<code><html>
    <body>
        <div id="page3">
            <ul>
            <li> 11 </li>
            <li> 12 </li>
            <li> 13 </li>
            <li> 14 </li>
            <li> 15 </li>
            </ul>
        </div>
    </body>
</html>
</code>```

Body:
update:

This question has some little xsl code in it !

I'm struggling with this problem for weeks (even months). I'm rather beginner through this xml/xslt area and I only need at least one working pagination technique for my xml data. I just can't seem to find just ONE working solution about this issue!!

I already state that, in my view, pagination is one of the most useful xml/xslt web topics and is one of the less discussed topics over the whole web. Only portions of it and most of them non-functional.
 Anyway, it's all about this stkovrflw question:

xslt xml table records pagination

where one could see my whole problem.
 It has been answered by michael.hor257k which I thank him very much.
 Point is it's only halfway working (as far as my requirement concerned)

I only need to display 5 nodes at a time, based on some parameter passed in which is the page number. I'll name it as

param name = "pagnmbr"

And, at first if this $pagnmbr is 1, that xslt will display:

01  02  03  04  05

Else, if it's 2 ($pagnmbr = 2), it will display:

06  07  08  09  10

And if same parameter become 3, the reading will be:

11  12  13  14  15

And so on. See, this kind of functionality I'm in very need for doing.

update:
I just did the following xslt code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="crrtPag" select="2"/>
 <xsl:variable name="rcrdsppag" select="5"/> <want 5 displaying records per page -->

 <xsl:template name="pag" match="/group/item">

   <xsl:if test="position()-$crrtPag = ($rcrdsppag - 1)*($crrtPag - 1)">
    <xsl:call-template name="display_pag">
    <xsl:with-param name="crrtPag" select="item[position() mod $rcrdsppag = 1]"/>
    </xsl:call-template>
   </xsl:if>

  </xsl:template>

  <xsl:template name="display_pag" match="item">
   <xsl:param name="crrtPag"/>
     <xsl:for-each select="//item">
       <xsl:if test="position()<=5">
         <xsl:value-of select="."/>
       </xsl:if>
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

But, as one could notice, it's kept showing only:

01 02 03 04 05

even if I put $pagnmbr = 2, or $pagnmbr = 3 .. pop up only the first 5 group items. It never shows up next node-set of

06  07  08 09  10

and so on ..

Please you guys help me with it !!!
 Thank you very much in advance.

Body:
Given:

XML

<group>
    <item> 01 </item>
    <item> 02 </item>
    <item> 03 </item>
    <item> 04 </item>
    <item> 05 </item>
    <item> 06 </item>
    <item> 07 </item>
    <item> 08 </item>
    <item> 09 </item>
    <item> 10 </item>
    <item> 11 </item>
    <item> 12 </item>
    <item> 13 </item>
    <item> 14 </item>
    <item> 15 </item>
    <item> 16 </item>
    <item> 17 </item>
    <item> 18 </item>
    <item> 19 </item>
    <item> 20 </item>
    <item> 21 </item>
    <item> 22 </item>
    <item> 23 </item>
    <item> 24 </item>
    <item> 25 </item>
    <item> 26 </item>
</group>

the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="pageSize" select="5" />
<xsl:param name="pageNumber" select="3" />

<xsl:template match="/group">
    <html>
        <body>
            <div id="page{$pageNumber}">
                <ul>
                    <xsl:variable name="start" select="$pageSize * ($pageNumber - 1) + 1" />
                    <xsl:for-each select="item[position() >= $start and position() &lt; $start + $pageSize]">
                        <li>
                            <xsl:value-of select="."/>
                        </li>
                    </xsl:for-each>
                </ul>
            </div>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

will return:

Result

<html>
    <body>
        <div id="page3">
            <ul>
            <li> 11 </li>
            <li> 12 </li>
            <li> 13 </li>
            <li> 14 </li>
            <li> 15 </li>
            </ul>
        </div>
    </body>
</html>


问:
Selenium select the first button that contains text matching
I am using the Firefox webdriver for Selenium to scrape a webpage that looks to be rendered with React on the client side. The classes in the rendered DOM look dynamically generated, and seem to change with every new request. There are also many button elements on the page, some of which are not in the viewport. So my strategy is to search for a way to click on a button that contains text that I enter using selenium. Several buttons will contain the text, and I want to just find the first such button.

Using selenium/xpath, how would I select the first button that contained the text E9 1QJ?

```<code><button>
    <div><svg ...> </div>
    <div>
        <div>London</div>
        <div>E9 1QJ</div>
    </div>
</button>

<button>
    <div><svg ...> </div>
    <div>
        <div>London Foo Bar</div>
        <div>E9 1QJ</div>
    </div>
</button>
</code>```

Thanks

答:
This should work:

```<code>{driver}.find_element_by_xpath("//button[div/div[text()='E9 1QJ']][1]")
</code>```

But keep in mind that a solution like this it is not very flexible and could break with a minimum change in the html structure.

Body:
I am using the Firefox webdriver for Selenium to scrape a webpage that looks to be rendered with React on the client side. The classes in the rendered DOM look dynamically generated, and seem to change with every new request. There are also many button elements on the page, some of which are not in the viewport. So my strategy is to search for a way to click on a button that contains text that I enter using selenium. Several buttons will contain the text, and I want to just find the first such button.

Using selenium/xpath, how would I select the first button that contained the text E9 1QJ?

<button>

<svg ...>

London

E9 1QJ

</button>

<button>

<svg ...>

London Foo Bar

E9 1QJ

</button>

Thanks

Body:
This should work:

{driver}.find_element_by_xpath("//button[div/div[text()='E9 1QJ']][1]")

But keep in mind that a solution like this it is not very flexible and could break with a minimum change in the html structure.


问:
Issue with MarkLogic QBE starts with functionality, QBE Date range in QBE API and Search filter criteria in Search API

I am exploring the MarkLogic QBE (query by example) API and in that would like to check how can i find the JSON documents which has "Name" key starts with some keyword such as "Hello*"? We can have $value parameter in QBE query for exact value match and $word parameter in QBE query for contain feature. But i want to use it for the feature of starts with functionality using wildcarded option in QBE . I have enabled 'trailing wildcard searches' index for database and also created field and field range index on "Name".
e.g.below query sample should return the result set with json documents which has Name starts with Hello and not contains Hello word

```<code>https://Server:port/v1/qbe?format=json&pageLength=10&start=1&options=search_option_advanced&directory=/json/&query={"$query":{"Name":{"$word":["Hello*"],"$exact":false,"$case-sensitive":false}}}

</code>```

答:
The query looks like it should work if Name is a JSON property and you've enabled wildcard searches for that property as described in:

http://docs.marklogic.com/guide/search-dev/wildcard#id_39731

If wildcard search is configured correctly but wildcard search isn't working in QBE, please file a support ticket.

So far as I know, wildcard searches don't work against range indexes.  The server does support value match lookups against a range index, but that's not a query.

Body:
I am exploring the MarkLogic QBE (query by example) API and in that would like to check how can i find the JSON documents which has "Name" key starts with some keyword such as "Hello*"? We can have $value parameter in QBE query for exact value match and $word parameter in QBE query for contain feature. But i want to use it for the feature of starts with functionality using wildcarded option in QBE . I have enabled 'trailing wildcard searches' index for database and also created field and field range index on "Name".
e.g.below query sample should return the result set with json documents which has Name starts with Hello and not contains Hello word

https://Server:port/v1/qbe?format=json&pageLength=10&start=1&options=search_option_advanced&directory=/json/&query={"$query":{"Name":{"$word":["Hello*"],"$exact":false,"$case-sensitive":false}}}


问:
Problems in scraping and saving file by eventlet
I can use evenlet to scrap img from website but failed to save them into domestic directory.
Following is the code. Any one is familiar with the I/O operation in tasklets model? Thanks</p>

```<code>import pyquery
import eventlet
from eventlet.green import urllib2

#fetch img urls............ works fine

print "loading page..."
html=urllib2.urlopen("http://www.meinv86.com/meinv/yuanchuangmeinvzipai/").read()
print "Parsing urls..."
d=pyquery.PyQuery(html)
count=0
urls=[]
url=''
for i in d('img'):
 count=count+1
 print i.attrib["src"]
 urls.append(i.attrib["src"])

def fetch(url):
 try:
  print "start feteching %s" %(url)
  urlfile = urllib2.urlopen(url)
  size=int(urlfile.headers['content-length'])
  print 'downloading %s, total file size: %d' %(url,size)
  data = urlfile.read()
  print 'download complete - %s' %(url)

##########################################
#file save just won't work

  f=open("/head2/"+url+".jpg","wb")
  f.write(body)
  f.close()
  print "file saved"
##########################################

  return data

 except:
  print "fail to download..."

pool = eventlet.GreenPool()

for body in pool.imap(fetch, urls):
  print "done"
</code>```

答:
Make sure that url is suitable as a filename e.g.:

```<code>import hashlib
import os

def url2filename(url, ext=''):
    return hashlib.md5(url).hexdigest() + ext # anything that removes '\/'

# ...
with open(os.path.join("/head2", url2filename(url, '.jpg')), 'wb') as f:
     f.write(body)
print "file saved"
</code>```

Note: you probably don't want to write your files to a top-level directory such as '/head2'.

You could also consider urllib.urlretrieve().

ContentLicense: CC BY-SA 3.0

Body:
I can use evenlet to scrap img from website but failed to save them into domestic directory.
Following is the code. Any one is familiar with the I/O operation in tasklets model? Thanks

import pyquery
import eventlet
from eventlet.green import urllib2

#fetch img urls............ works fine

print "loading page..."
html=urllib2.urlopen("http://www.meinv86.com/meinv/yuanchuangmeinvzipai/").read()
print "Parsing urls..."
d=pyquery.PyQuery(html)
count=0
urls=[]
url=''
for i in d('img'):
 count=count+1
 print i.attrib["src"]
 urls.append(i.attrib["src"])

def fetch(url):
 try:
  print "start feteching %s" %(url)
  urlfile = urllib2.urlopen(url)
  size=int(urlfile.headers['content-length'])
  print 'downloading %s, total file size: %d' %(url,size)
  data = urlfile.read()
  print 'download complete - %s' %(url)

##########################################
#file save just won't work

  f=open("/head2/"+url+".jpg","wb")
  f.write(body)
  f.close()
  print "file saved"
##########################################

  return data

 except:
  print "fail to download..."

pool = eventlet.GreenPool()

for body in pool.imap(fetch, urls):
  print "done"

Body:
Make sure that url is suitable as a filename e.g.:

import hashlib
import os

def url2filename(url, ext=''):
    return hashlib.md5(url).hexdigest() + ext # anything that removes '\/'

# ...
with open(os.path.join("/head2", url2filename(url, '.jpg')), 'wb') as f:
     f.write(body)
print "file saved"

Note: you probably don't want to write your files to a top-level directory such as '/head2'.

You could also consider urllib.urlretrieve().